Re: HEX to Integer conversion in pro*c or pl*sql
Date: Fri, 18 Feb 1994 13:19:50 -0500
Message-ID: <l.carl.pedersen-180294131950_at_kip-sn-248.dartmouth.edu>
In article <CL87y0.10y0_at_empros.com>, btreuman_at_empros.com (Bob Treumann) wrote:
>
> Does anyone have a C routine that will convert data stored as varchar2
> into an integer, assuming the data is a valid hex number?
>
> It seems I should be able to do this with HEXTORAW function somehow,
> but I can't make it work...
>
> I have both 2-character (e.g. 9E) and four character data (7FFA) to
> convert.
>
> I will gladly post the correct answer ...
>
>
Not sure why HEXTORAW doesn't work, but you should be able to do it with a loop like this:
function val(string in char) return number is
result number := 0;
begin
for i in 1..length(string) loop
result := 16 * result + decode(substr(string,i,1),
'A',10,'B',11,'C','12','D','13','E','14','F','15', substr(string,i,1));
end loop;
end val;
Obviously, that's in PL/SQL, but if you really need it in some other language, it should be easy to trabnslate.
This is based on the general algorithm for converting to a number from a string representing a number in any base. It should work regardless of the string length.
I did *not* test this. Let me know if you have trouble making it work. Received on Fri Feb 18 1994 - 19:19:50 CET