Xquery中Ascii到Hex的转换



我需要使用XQuery将ASCII转换为十六进制字符串,请帮助提供任何相应的参考或代码片段来实现此功能,例如,我有字符串:1239565447854,我需要转换为Hex:31323339353635343437383534

您可以使用string-to-codepoints()来获取每个字符的数字代码点。对于每个代码点,使用idiv确定其可被16整除的次数,并使用mod运算符确定余数。对于这两个数字,通过在16个字符的序列中按位置查找来获得它们的十六进制值,然后将结果相加:

declare function local:codepoint-to-hex($codepoint as xs:integer) as xs:string {
let $hex-index := ("0123456789ABCDEF" => string-to-codepoints()) ! codepoints-to-string(.)
let $base16 := $codepoint idiv 16
let $remainder := $codepoint mod 16
return 
string-join((
$hex-index[$base16 + 1],
if ($remainder = 0) 
then ()
else $hex-index[$remainder + 1]
), "")
};
declare function local:string-to-hex($str as xs:string) as xs:string {
string-join( string-to-codepoints($str) ! local:codepoint-to-hex(.), "")
};
local:string-to-hex('1239565447854')

最新更新