Thread: Float to byte array

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    http://cboard.cprogramming.com/showthread.php?t=87775
    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.

  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, 06: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, 02:19 AM