sockets: htonl question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Impelluso

    sockets: htonl question


    Hello,

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

    I am aware of Beej's (Brian "Beej Jorgensen" Hall) implementation:


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

    i =read(descripto r, (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


  • mlimber

    #2
    Re: sockets: htonl question

    On Jul 23, 12:16 pm, Tom Impelluso <impel...@attil a.sdsu.eduwrote :
    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/ht....html#htonsman
    >
    Yet, I devised my own kludge:
    ------------------------
    uint32_t data, data1;
    float num1;
    >
    i =read(descripto r, (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#seri alizationwhen he mentions different binary formats
    for floating point numbers.</OT>

    Cheers! --M

    Comment

    Working...