Friday, April 24, 2009

Stupid SQL, First Edition - turning numbers into hex strings

The following Oracle SQL command will take a 64-bit positive number and emit a 16-character hexadecimal string.

select
lpad(rawtohex(chr(floor(MyNumber / 4294967296))), 8, '0') ||
lpad(rawtohex(chr(mod(MyNumber, 4294967296))), 8, '0')
from dual

I'll post an updated solution for negative numbers when I get a chance. (Basic idea: add 2^64, then mod by 2^64.)

No comments:

Post a Comment