X-Original-To: pgsql-interfaces-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 8D6FF5282C for ; Wed, 18 May 2005 08:10:00 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 24403-03 for ; Wed, 18 May 2005 11:09:50 +0000 (GMT) Received: from fzkmail2.fzk.de (fzkmail2.fzk.de [141.52.27.52]) by svr1.postgresql.org (Postfix) with SMTP id A428052803 for ; Wed, 18 May 2005 08:09:48 -0300 (ADT) Received: FROM resy5.fzk.de BY fzkmail2.fzk.de ; Wed May 18 13:09:44 2005 +0200 Received: from rodos.fzk.de (resy6.fzk.de [141.52.68.10]) by resy5.fzk.de (8.12.7/8.12.7/SuSE Linux 0.6) with ESMTP id j4IB9hsG025899; Wed, 18 May 2005 13:09:43 +0200 Message-ID: <428B2276.7C2FFF55@rodos.fzk.de> Date: Wed, 18 May 2005 13:09:42 +0200 From: Christoph Haller X-Mailer: Mozilla 4.77 [en] (X11; U; HP-UX B.10.20 9000/780) X-Accept-Language: en MIME-Version: 1.0 To: Volkan YAZICI Cc: PostgreSQL Interfaces Subject: Re: Network Byte Order Coercion References: <7104a73705051603121f1c35ab@mail.gmail.com> Content-Type: text/plain; charset=iso-8859-2 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS - amavis-milter (http://www.amavis.org/) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.024 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200505/38 X-Sequence-Number: 5541 Volkan YAZICI wrote: > > Hi, > > In libpq example program 3 (testlibpq3.c), an int4 field is converted > to host byte order: > > {{{ > /* Get the field values (we ignore possibility they are null!) */ > iptr = PQgetvalue(res, i, i_fnum); > > /* > * The binary representation of INT4 is in network byte order, > * which we'd better coerce to the local byte order. > */ > ival = ntohl(*((uint32_t *) iptr)); > }}} > > (As I saw while reading "An Essay on Endian Order" [1]) I'm not so > familiar with byte orders, but what's the point of coercion in here? > Should we do it in every integer field we retrieved? What's the > [dis]advantages of this? I'd be so appreciated for any explanation. > > [1] http://www.cs.umass.edu/~verts/cs32/endian.html > > Regards. > As long as binary results are retrieved, PG delivers the data always in network byte order (big endian), no matter what byte order the processor is actually using. On a machine using big endian, functions like ntohl do nothing. On little endian systems, ntohl reverses the byte order, so the result is correct. So, if you want to write portable code, you'll have to consider the byte order, otherwise the same code would produce different results on different machines (PC, MAC). In the end it's not at all a question of advantages versus disadvantages, you'll just have to do it. HTH Regards, Christoph