sockets: htonl question

T

Tom Impelluso

Hello,

I am aware of the need for ntohl and htonl.
But floats?

I am aware of Beej's (Brian "Beej Jorgensen" Hall) implementation:
http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#htonsman

Yet, I devised my own kludge:
------------------------
uint32_t data, data1;
float num1;

i =read(descriptor, (char *) &data, sizeof(uint32_t));
data1 = ntohl(data);
num1 = *(float*) ( &data1);
--------------------------

This works for me; always has!
Now, mind you, I have done scientific coding.
And in an enviroment with extremley well-behaved floats,
where round-off has never been an issue.
And very little exceptions....

So...

Why does my simpler implementation work?
What problems could it cause?
Friends have advised me to use Jorgensen, but I do not see the need.
(I also have not gotten it to work)

Thanks,
tom
 
M

mlimber

Hello,

I am aware of the need for ntohl and htonl.
But floats?

I am aware of Beej's (Brian "Beej Jorgensen" Hall) implementation:http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#htonsman

Yet, I devised my own kludge:
------------------------
uint32_t data, data1;
float num1;

i =read(descriptor, (char *) &data, sizeof(uint32_t));
data1 = ntohl(data);
num1 = *(float*) ( &data1);
--------------------------

This works for me; always has!
Now, mind you, I have done scientific coding.
And in an enviroment with extremley well-behaved floats,
where round-off has never been an issue.
And very little exceptions....

So...

Why does my simpler implementation work?
What problems could it cause?
Friends have advised me to use Jorgensen, but I do not see the need.
(I also have not gotten it to work)

Sockets are technically off-topic here (cf. <http://www.parashift.com/c
++-faq-lite/how-to-post.html#faq-5.9>).

<OT>I think "Beej" addresses your question as to why in section 6.4
<http://beej.us/guide/bgnet/output/html/singlepage/
bgnet.html#serialization> when he mentions different binary formats
for floating point numbers.</OT>

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top