pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oleg Semykin <oss-dev@rambler.ru>
To: pgsql-interfaces@postgresql.org
Subject: Get double values from binary cursor
Date: Thu, 15 Dec 2005 16:10:05 +0300
Message-ID: <43A16B2D.20203@rambler.ru> (raw)

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.



view thread (2+ messages)  latest in thread

Message-ID: <43A16B2D.20203@rambler.ru>
Permalink:  ../43A16B2D.20203@rambler.ru/
Also on:    postgresql.org/message-id/43A16B2D.20203@rambler.ru

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: oss-dev@rambler.ru
  Subject: Re: Get double values from binary cursor
  In-Reply-To: <43A16B2D.20203@rambler.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox