Decimal to Hex converter

Hi,

Has anyone written dxl code to convert a decimal to hexidecimal and is willing to post here.

Thanks
OurGuest - Thu Feb 03 11:42:13 EST 2011

Re: Decimal to Hex converter
Mathias Mamsch - Thu Feb 03 12:17:07 EST 2011

Hope this helps. Note that the intOfHex is not optimized for performance ;-). Regards, Mathias
 

char gar_hex_chars[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}
 
string hex (int memory) {
    string result = ""
        if memory == 0 then return "0x0" 
        while (memory > 0) {
                result = gar_hex_chars[memory & 0xF] result
                memory /= 16
        } 
        return "0x" result
}
 
// intOfHex() takes a string with OR without a leading "0x",  (i.e. "0xC4F", "0x02EA", "C4F", "02EA") 
// NOTE: DXL limits the value of a integer to MAX_INT=2147483647
int intOfHex( string s ) {
        if( "0x" == s[0:1] ) {
                return intOf( realOf( eval_ "return_ (" s ") \"\"" ) )
        } else {
                return intOf( realOf( eval_ "return_ (0x" s ") \"\"" ) )
        }
}

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS