【问题标题】:Hex to Ascii conversion in XqueryXquery 中的 Hex 到 Ascii 转换
【发布时间】:2018-03-08 15:51:07
【问题描述】:

我需要使用 xquery 将十六进制字符串转换为 ASCII,请帮助提供任何相应的参考或代码 sn-ps 以实现此功能。

感谢您的帮助..!!!

【问题讨论】:

  • 不是所有的十六进制符号都是ASCII字母吗?您在寻找十六进制和 ASCII 之间的哪种映射?
  • 请尝试更好。什么是“十六进制字符串”?
  • 最简单的方法是将其转换为 xs:hexBinary 并将二进制文件作为字符串读取,但并非所有 xquery 处理器都可以执行第二步

标签: xml xslt xquery


【解决方案1】:

如果你想解码一个十六进制编码的字符串,你可以使用fn:string-to-codepoints($string)fn:codepoints-to-string($codepoints)来解构/重构字符串:

declare function local:hex-digit($digit as xs:integer) as xs:integer {
  (: range '0'..'9' :)
  if(48 le $digit and $digit lt 58) then $digit - 48
  (: range 'a'..'f' :)
  else if(97 le $digit and $digit lt 103) then $digit - 87
  (: everything else :)
  else fn:error((), 'Illegal character: ' || $digit)
};

declare function local:hex-to-string($hex as xs:string) as xs:string {
  let $n := fn:string-length($hex)
  let $digits := fn:string-to-codepoints(lower-case($hex))
  return fn:codepoints-to-string(
    for $pos in 1 to $n idiv 2
    let $hi := $digits[2 * $pos - 1],
        $lo := $digits[2 * $pos]
    return 16 * local:hex-digit($hi) + local:hex-digit($lo)
  )
};

然后local:hex-to-string("68656c6c6f20776f726c6421")返回字符串"hello world!"

【讨论】:

    猜你喜欢
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2011-08-09
    • 2019-09-13
    • 2012-11-02
    • 1970-01-01
    • 2014-08-25
    相关资源
    最近更新 更多