Byte ordering

Pedro Gonnet pedro@vis.ethz.ch
Tue Sep 30 14:50:00 GMT 2003


Hello Lazar,

I think the functions you're looking for are 

	uint32_t htonl(uint32_t hostlong);
	uint32_t ntohl(uint32_t netlong);

These operate on 32-bit values, so you have to do something like

	double hton_double ( double in ) {

	   double res;
	   unsigned int *in , *out = (unsigned int *)(&res);

	   out[0] = htonl(in[0]);
	   out[1] = htonl(in[1]);

	   return res;

	   }

Beware that on some machines (or with data dumped by programs compiled
with Portland's byte-swapping fortran compiler), the 32-bit values are
also swapped, in which case you would have to write:

	out[0] = htonl(in[1]);
	out[1] = htonl(in[0]);

Hope this helps...

Pedro




On Tue, 2003-09-30 at 16:41, Z F wrote:
> Hello everybody,
> 
> 
> Even though this question is not directly related to GSL, indirectly
> --- it is.
> 
> I have a scientific calculation program (which uses GSL) but most
> importantly it uses doubles. I need to transfer those data to another
> computer. The problem is that if the two computers have different byte
> ordering, I have to do something special about the data. I understand
> that there is a network standard for shorts and for ints to serve this
> purpose. I could not find any standards to transfer doubles/floats over
> net. I the past I could live with printf()-type things and convert all
> doubles to strings and pass strings since ASCII is more universal.
> This, however, increases the data size by a factor of three.
> 
> The current problem I am working on has data output rate of about
> 5-15MBytes/sec and increasing it by factor of three is not feasible.
> 
> Could someone, please, point me in the right direction? Should I
> give-up  on portability of my code and assume/hope that both ends use
> the same 
> byte ordering?
> 
> I know that this is not directly related to GSL, but all the network
> people do not seem to care about doubles so I have to turn to
> "scientific" network programming....
> 
> Thank you very much for your input,
> 
> Lazar
> 
> __________________________________
> Do you Yahoo!?
> The New Yahoo! Shopping - with improved product search
> http://shopping.yahoo.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://sourceware.org/pipermail/gsl-discuss/attachments/20030930/1b3ee55d/attachment.sig>


More information about the Gsl-discuss mailing list