Hi, all.
i want to get a double values from binary cursor
in libpq < 7.4 it was simple
memcpy(&dfVal, PQgetvalue(...), sizeof(dfVal));
but now we have to convert data from MSB to LSB format
it is ok for int
but how to convert a double
i'am tried to write a simple program, but it does not work ....
#include <libpq-fe.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
typedef unsigned int int4;
typedef double float8;
int main()
{ if ( sizeof(int4) * 2 != sizeof(float8) ) { printf("Error"); return 1; }
PGconn * conn = PQconnectdb("dbname=test host=localhost user=trusted"); PGresult * res;
PQclear( PQexec(conn, "begin; declare my_cur binary cursor for
select 123.123;") ); res = PQexec(conn,"fetch 1 from my_cur");
float8 dfVal; float8 dfOrig = 123.123;
union { float8 f; int4 n[2]; } swap;
memcpy(&(swap.n), PQgetvalue( res, 0, 0 ), sizeof(int4) * 2 );
int4 tmp = ntohl(swap.n[0]); swap.n[0] = ntohl(swap.n[1]); swap.n[1] = tmp;
dfVal = swap.f;
printf("ret = [%f]\norig = [%f]\n", dfVal, orig);
PQclear(res); PQfinish(conn); return 0;
}
Is something wrong?
Thank for any help.