Thread: Float, Double, to bytes?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    38

    Float, Double, to bytes?

    I am currently using this way to cast a double or float to an array of bytes (uint8_t) and back:

    To bytes:
    Code:
    double data = 5.5;
    
    std::queue<uint8_t> ndata;
    uint8_t* tmp = (uint8_t*)&data;
        
    for (int i = 0; i < sizeof(double); i++)
        ndata.push(tmp[i]);
        
    return ndata;
    And back:
    Code:
    double data = 0.0;
    
    std::queue<uint8_t> ndata;
    uint8_t* tmp = (uint8_t*)&data;
    
    for (int i = 0; i < sizeof(double); i++)
        tmp[i] = ndata.front(), ndata.pop();
    
    return ndata;
    This is butt ugly code though, is there any other way to do this properly in a C++ way (or with Boost)? The resulting bytes will be eventually sent over a network connection (loopback interface actually).

    I am working with GCC on Linux x64.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you need to store it in a queue, though?
    Isn't a pointer enough?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    The resulting bytes will be eventually sent over a network connection
    Careful.

    Endianness - Wikipedia, the free encyclopedia

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Quote Originally Posted by Elysia View Post
    Do you need to store it in a queue, though?
    Isn't a pointer enough?
    Various other kinds of object and types will be stored in the queue, and then together send across the network.

    Quote Originally Posted by cyberfish View Post
    I am aware of this. i have solved this problem with integers, but i do not know how to solve this with floats or doubles, since you can't use bit shifting on them.

    The transmitted data will stay on the same machine, but i don't think that's a valid reason to forget endianness.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Careful.
    Not everyone uses IEEE754
    Survey of Floating-Point Formats at MROB
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    37
    One thing you have to consider is some machines are little-endian and other's are big-endian. I think that even goes for floats and doubles too. This means that if you move your bite array from one machine to another and reconstruct it, the number will be wrong because the bytes will be reversed. That's not to mention the fact that not all machines use the same floating point format. However that's getting to be less of a problem these days with IEEE standards and such. If I recall there used to be a few machines that didn't even use two's compliment for integers, but this isn't something to worry about these days.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with program
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 06-10-2007, 08:05 PM
  2. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  3. Im stuck....
    By dAzed in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 04:50 PM
  4. writing double quote & comma delimited files
    By emt in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2003, 12:28 AM
  5. How to display 23 bytes long double answer in C?
    By CLIE_ZETA in forum C Programming
    Replies: 3
    Last Post: 11-18-2001, 12:11 PM