pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Network Byte Order Coercion
2+ messages / 2 participants
[nested] [flat]

* Network Byte Order Coercion
@ 2005-05-16 10:12 Volkan YAZICI <volkan.yazici@gmail.com>
  2005-05-18 11:09 ` Re: Network Byte Order Coercion Christoph Haller <ch@rodos.fzk.de>
  0 siblings, 1 reply; 2+ messages in thread

From: Volkan YAZICI @ 2005-05-16 10:12 UTC (permalink / raw)
  To: pgsql-interfaces

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.



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Network Byte Order Coercion
  2005-05-16 10:12 Network Byte Order Coercion Volkan YAZICI <volkan.yazici@gmail.com>
@ 2005-05-18 11:09 ` Christoph Haller <ch@rodos.fzk.de>
  0 siblings, 0 replies; 2+ messages in thread

From: Christoph Haller @ 2005-05-18 11:09 UTC (permalink / raw)
  To: Volkan YAZICI <volkan.yazici@gmail.com>; +Cc: pgsql-interfaces

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



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2005-05-18 11:09 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-05-16 10:12 Network Byte Order Coercion Volkan YAZICI <volkan.yazici@gmail.com>
2005-05-18 11:09 ` Christoph Haller <ch@rodos.fzk.de>

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