Xquery中的Hex到Ascii转换

时间:2018-03-08 15:51:07

标签: xml xslt xquery

我需要使用xquery将Hex字符串转换为ASCII,请帮助提供任何相应的参考或代码片段以实现此功能。

感谢您的帮助.. !!!

1 个答案:

答案 0 :(得分: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!"