Converting a long long to an int8.
Posted in 2004
Topics: Server Administration
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 ?
Many tahnks,
Andrew H.
sending to informix-list
"Andrew Hardy" <Andrew.Hardy@marconi.com> wrote in message news:cd3k30$ab$1@news.xmission.com...
>
> 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 ?
>
It seems you are talking about ESQL/C.
here is an easy way to do this.
write a function in informix to convert int8 to char(19).
create function int8tochar(p_in int8) returning char(19)
define w char(19); let w = p_in ;
return w ;
end function
In ESQL/C you can retrieve this field via the above function
into a C host variable of char[20]
$ select int8tochar(field_name)
into :w_c_variable
from ..
where ...
and then use atoll to convert it to C long long.
This is the only way I use in 9.21. May be in later versions there
is a better way of doing it.
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/