Hi, |
Re: Decimal to Hex converter
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
|