XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Dominic VieiraSubject: Convert decimal to Hex.
Author: Dominic Vieira
Date: 28 Jun 2007 02:05 PM
Hi, is there a way to cast a decimal value to a hex value? For example, Decimal 177 convert to hex B1.

Thanks,
Dominic

Postnext
Alberto MassariSubject: Convert decimal to Hex.
Author: Alberto Massari
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

Posttop
Rittik BanikSubject: 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)

 
Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.