Float to byte array

This is a discussion on Float to byte array within the C Programming forums, part of the General Programming Boards category; I want to convert a float into an array of bytes (char) to transmit over a non-canonical serial connection. I ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    4

    Float to byte array

    I want to convert a float into an array of bytes (char) to transmit over a non-canonical serial connection. I want to use the 4 byte structure of the original float that way I do not have to worry about variable lengths and still get full precision.

    One method I have stumbled upon is this CCS compiler FAQ but I would like to try to get my approach (already implemented) working before I redo the code. Especially since I'm not sure if the PIC C compiler works the same as the ARM Linux gcc compiler in this scenario.

    I attempted to use a union to do the conversion like so:

    Code:
    union
    {
        char bytes[4];
        float val;
    } bfconvert;
    
    float bobsuruncle = 7.314239;
    char output[4];
    
    bfconvert.val = bobsuruncle;
    output[0] = bytes[0];
    output[1] = bytes[1];
    output[2] = bytes[2];
    output[3] = bytes[3];
    on the other side of the serial line I perform the opposite process:

    Code:
    union
    {
        char bytes[4];
        float val;
    } bfconvert;
    
    float whosuruncle;
    char input[4];
    
    // Read bytes off serial connection
    
    bfconvert.bytes[0] = input[0];
    bfconvert.bytes[1] = input[1];
    bfconvert.bytes[2] = input[2];
    bfconvert.bytes[3] = input[3];
    whosuruncle = bfconvert.val;
    The resulting output varies wildly with small changes in the actual float value (I don't have any examples off hand unfortunately). The best example I can give is that a value that should be very close to zero (no higher than 0.1, but generally lower) comes out as:
    680667722740137984.000000

    Thanks in advance,
    osuee

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Trouble reading a binary file as float values - I need the EXPERTs' HELP in C++ code!
    Check that the receiver is using the same endian as the sender, and adjust accordingly.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    4

    sadly

    Thank you for your response. I'll do some more testing with the endianness (though my int16's SEEM to come across fine), the post you linked was very helpful.

    Sorry for not looking thoroughly through the forums archives before posting.

    *edit* After digging into my compiler's tools I found out that Microchip PICs use a different float format than the IEEE standard. You learn something new everday...

    Thanks again,
    osuee
    Last edited by osuee; 03-26-2007 at 05:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 05:36 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  5. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21