[Info-ingres] Convert hex string to an integer
Martin Bowes
martin.bowes at ndph.ox.ac.uk
Wed Aug 11 13:21:25 UTC 2021
Hi All,
There has got to be an easier way....
Given a string which represents a hex value, convert it to an integer of a given size.
The only solution I've come up with is by making an OME function. See attached.
Here is how I would describe that function...
hex2int(hexString, integerSize)
Convert the indicated hex string into a signed integer of the given size. Case is ignored.
Two's complement is respected and hence when converting to integer1 the string 'FF' becomes -1. However the same string converted to integer2 becomes 255.
The hex string may be prefixed with '0x', '+0x', or '-0x'. But in the case of '-0x' there is the possibility of a double negative if the string converts to a negative integer.
And here is some sample output...
select hex2int('FFFD', 8)\g
┌──────────────────────┐
│col1 │
├──────────────────────┤
│ 65533│
└──────────────────────┘
(1 row)
select hex2int('FFFD', 22)\g
E_OME001 hex2int(): Invalid size '22' specified. It must be one of 1, 2,
4, 8
(Tue Aug 10 14:47:58 2021)
select hex2int('FFFD', 2)\g
┌──────┐
│col1 │
├──────┤
│ -3│
└──────┘
(1 row)
select hex2int('garbage', 8)\g
Executing . . .
E_OME001 hex2int(): Non hex character 'g' found in string.
(Wed Aug 11 14:08:29 2021)
So the questions are:
1. Can anyone do this in simple SQL? If so, how?
2. Could people have a quick look over the C code and give me some feedback please. I'm not a C coder and after compiling with no errors I'm not too sure what more I have to do.
Thanks,
Marty
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.planetingres.org/pipermail/info-ingres/attachments/20210811/ec4de077/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: hex2int.c
URL: <https://lists.planetingres.org/pipermail/info-ingres/attachments/20210811/ec4de077/attachment.c>
More information about the Info-ingres
mailing list