I have a requirement to convert the ASCII to Hex String using XQuery, Please help to provide any respective reference or code snippets to achieve this functionality, by example I have then string: 1239565447854
and I need convert to hex: 31323339353635343437383534
You can use string-to-codepoints()
to get the numeric codepoint for each character. For each codepoint, determine how many times it is divisible by 16 using idiv
, and the remainder using the mod
operator. For both of those numbers, get their hex value by looking up by position in a sequence of the 16 characters and then join the results:
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')