Convert hex string to numeric

Teradata Employee

Convert hex string to numeric

One of my input tables contains strings with hexadecimal value. I have to convert these entries to numeric values.
Is there an easy way to do that?

Thanks in advance!

Best regards,

Re: Convert hex string to numeric

Not really! It is really down to poor definition if you are holding numbers in hex strings.

You do not say how long the strings are. The example below show how to convert a 4 character string to a number - you can extend it for a few more characters if necessary. (I presume you are not converting long hex strings, otherwise you will have to put them in float and possibly lose precision.)

Create volatile table hexs (Hexstring char(4) not null)
On commit preserve rows;

Insert into Hexs Values ('1234');

,((((((Index('0123456789abcdef',Substr(Hexstring,1,1)) -1) * 16 ) +
((Index('0123456789abcdef',Substr(Hexstring,2,1)) -1) ))* 16) +
((Index('0123456789abcdef',Substr(Hexstring,3,1)) -1) ))* 16) +
Index('0123456789abcdef',Substr(Hexstring,4,1)) -1
From Hexs