declare function functx:chars
( $arg as xs:string? ) as xs:string* {
for $ch in string-to-codepoints($arg)
return codepoints-to-string($ch)
} ;
declare function functx:index-of-string
( $arg as xs:string? ,
$substring as xs:string ) as xs:integer* {
if (contains($arg, $substring))
then (string-length(substring-before($arg, $substring))+1,
for $other in
functx:index-of-string(substring-after($arg, $substring),
$substring)
return
$other +
string-length(substring-before($arg, $substring)) +
string-length($substring))
else ()
} ;
declare function local:power($a as xs:double, $b as xs:integer) as xs:double {
if($b < 0) then 1 div local:power($a, abs($b))
else if($b = 0) then 1
else if($b = 1) then $a
else local:power($a, $b mod 2) * local:power($a * $a, $b idiv 2)
};
declare function local:convertToDecimal($hexstr as xs:string , $value as xs:double) as xs:double
{
let $digits as xs:string:= "123456789ABCDEF"
let $length as xs:integer := fn:string-length($hexstr)
let $hexstr2 as xs:string* := functx:chars($hexstr)
let $value as xs:double := local:power( 16.00,(fn:string-length($hexstr) - 1 )) * functx:index-of-string($digits ,$hexstr2[1]) + $value
return
if($length = 1)
then $value
else
local:convertToDecimal(fn:substring($hexstr,2),$value)