Thread: More unions and bytes

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    More unions and bytes

    To assist in solving a problem that allowed for storing unsigned char data in the LS Byte of a specific float variable, the following approach was suggested:

    union u
    {
    struct
    {
    float f1;
    float f2;
    } f;
    unsigned char bytes[8];
    };
    --------------------------------------------------------------------------------
    ;
    union u unie;
    unie.f.f2 = 1.2345;
    unie.bytes[3] = unie.bytes[7];

    Writing the data to a binary file that is the sizeof(unie) seemed sufficient for my program. I was able to read back the binary data (reading the union itself).

    HOWEVER, if I'm limited to reading this data back into only a structure that contains two float variables, how do I access the embedded unsigned char value?

    Essentially, a second program, that is legacy software, utilizes a structure that will need to read this new data format, without changing all the structure member names.

    The structure is as follows:

    struct
    {
    float v1;
    float v2;
    }

    So, I have to read my new binary file, equate the structure floats to the binary data, create an unsigned char variable, and extract its information from the float variable (in the previous example, f2).

    I'm having difficulty extracting the unsigned char information.

    How do I view/extract the unsigned char data?

    Many thanks for the suggestions!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >How do I view/extract the unsigned char data?

    Here is one example using the union.
    Code:
    #include <stdio.h>
    
    void foo(void)
    {
        union u
        {
            struct
            {
                float f1;
                float f2;
            } f;
            unsigned char bytes[8];
        } unie = {{1.23F,4.56F}};
        size_t i;
        for ( i = 0; i < sizeof(unie); ++i )
        {
            printf("%02X ", (unsigned)unie.bytes[i]);
        }
        putchar('\n');
    }
    Here is another way.
    Code:
    void dump(const void *buffer, size_t size)
    {
        const unsigned char *ch = buffer;
        size_t i;
        for ( i = 0; i < size; ++i )
        {
            printf("%02X ", (unsigned)*ch++);
        }
        putchar('\n');
    }
    
    int main(void)
    {
        float value[2] = {1.23F,4.56F};
        size_t i;
    
        foo();
    
        for ( i = 0; i < sizeof(value)/sizeof(*value); ++i )
        {
            printf("value[%lu] = %f, ", (unsigned long)i, value[i]);
            dump(&value[i], sizeof(value[i]));
        }
    
        return 0;
    }
    My output is as follows.
    Code:
    A4 70 9D 3F 85 EB 91 40
    value[0] = 1.230000, A4 70 9D 3F
    value[1] = 4.560000, 85 EB 91 40

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double's and floats, i need their bytes.
    By Florian in forum C++ Programming
    Replies: 26
    Last Post: 07-08-2008, 05:42 PM
  2. Read Bytes From File Into Integer
    By ChadJohnson in forum C Programming
    Replies: 28
    Last Post: 08-16-2004, 11:57 AM
  3. Unions and bytes
    By wildcat in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 09:02 AM
  4. unions
    By mickle in forum C Programming
    Replies: 6
    Last Post: 02-27-2003, 11:46 PM
  5. unions problems!
    By nextus in forum C++ Programming
    Replies: 3
    Last Post: 01-12-2003, 04:04 AM