|
next
|
 Subject: Convert decimal to Hex. Author: (Deleted User) Date: 29 Jun 2007 04:15 AM
|
Hi Dominic,
there is not a built-in function to do that, but I wrote this user-defined function to do the job
declare function local:convertToHex($number as xs:integer) as xs:string
{
let $hexDigits:=('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
return
if($number < 16) then
$hexDigits[$number + 1]
else
fn:concat(local:convertToHex($number idiv 16), local:convertToHex($number mod 16))
};
Hope this helps,
Alberto
|
top
|
 Subject: Convert decimal to Hex. Author: Rittik Banik Date: 22 Dec 2011 10:25 AM
|
Hi , this is what could be used for Hex to Decimal :
**********************************************************************
declare namespace functx = "http://www.functx.com";
declare namespace op ="http://www.w3.org/2001/12/xquery-operators";
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)
};
local:convertToDecimal("A15", 0.00)
|
|
|
|