Re: Converting a long long to an int8.
Posted in 2004
Sorry to be a pain, my question was actually about converting an unsigned
long long TO an int8.
For that, would I do something like the code below then ?
What sign would I set ?
What is the difference between int8 and ifx_int8_t ?
Thanks again for your help.
Andrew H.
void someSQLFunction()
{
EXEC SQL BEGIN DECLARE SECTION;
char sqlLine[SQL_SIZE];
int a;
int b;
ifx_int8_t testInt8;
EXEC SQL END DECLARE SECTION;
unsigned long long x;
x = 0xF0F0F0F0F0F0F0F0;
testInt8.data[0] = (x & 0xFFFFFFFF00000000) >> 32;
testInt8.data[1] = (x & 0x00000000FFFFFFFF);
/*
Not sure what to set this to. It seems to me that if, as is
suggested below, any 64 bit content can be
positive or negative, then you will end up with values bigger than
C can hold in 64 bits. But the Guide
seems to indicate by the range it states that you can only hold
signed values not unsigned!
*/
testInt8.sign = ??;
sprintf (sql_line, "INSERT INTO TAB1 (f1, f2, f3) VALUES
(?,?,?)");
EXEC SQL PREPARE writeIt FROM :sql_line;
EXEC SQL EXECUTE writeIt USING :a, :b, :testInt8;
.....
.....
}
|---------+---------------------------->
| | Jonathan Leffler |
| | <jleffler@earthli|
| | nk.net> |
| | Sent by: |
| | owner-informix-li|
| | st@iiug.org |
| | |
| | |
| | 15/07/2004 06:08 |
| | Please respond to|
| | Jonathan Leffler |
| | |
|---------+---------------------------->
>--------------------------------------------------------------------------------------------------------------------------------------------------|
| |
| To: informix-list@iiug.org |
| cc: |
| Subject: Re: Converting a long long to an int8. |
>--------------------------------------------------------------------------------------------------------------------------------------------------|
Andrew Hardy wrote:
> My guide seems to tell me the functions to convert to an int8 the types
> long and double, but not a long long, which on our machine is the type
for
> an 8 byte value.
>
> I actually want to store an unsigned long long. I don't mind if ifx
thinks
> it's signed or shows it as signed in dbaccess or if I have to cast it to
> signed tpo store it so long as when I get it back internally it still has
> the same bits set.
>
> But I can't seem to see the function to do this.
>
> Should i be using something other than an int8 ? A serial8 ?? Or
> something. Which function do I use ?
You need to write your own - until further notice. You could try
checking for undocumented functions starting ifx_int8, but...
The structure is documented in int8.h. Your sign bit is in the sign
field; one of the two 32-bit unsigned integers contains 31 bits, the
other 32 bits. You can therefore reassemble them into a 64-bit long
using:
long long ifx_int8tolonglong(ifx_int8_t *in)
{
long long out = in->data[0] << 32 | in->data[1];
if (in->sign < 0)
out = - out;
return(out);
}
You'd need to experiment to see whether the shift is 32 or 31, and
whether element 0 or element 1 is the more significant. I'm also
ignoring NULL.
The inverse conversion is not very much harder, of course.
--
Jonathan Leffler #include <disclaimer.h>
Email: jleffler@earthlink.net, jleffler@us.ibm.com
Guardian of DBD::Informix v2003.04 -- http://dbi.perl.org/
sending to informix-list