Union as a converter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rabidmonkey
    New Member
    • Feb 2007
    • 13

    Union as a converter

    Hi all, briefly I am writing a client server app that communicates over shared mem (which is all fine) but I seemed to be stumped on using a union to convert from float to another type. ultimatly what I am trying to achieve is the server writes a 32 bit float to shared memory and the client accesses this and interperets it as an array of boolean chars eg "001101011. ..." I have this rough code that uses a union from float to char[4] (not binary i know but a start) and when I run it the displayed string is different each time?? any ideas?

    thanks in advance

    Code:
    #include <stdio.h>
    int main()
    {
    
      union un
      {
        float fl;
        unsigned char str4[4];
      }   array1;
    
      unsigned char temp[4];
      array1.fl=1256.667;
      printf("\n%f   Size:%d\n",array1.fl,sizeof(array1.fl));
      printf("\n%s  Size:%d\n",array1.str4,sizeof(array1.str4));
    }
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Hi. A union variable can hold only one member variable at a time so, in order to achieve your goal, you will need to declare two different variables of type un.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      An array of characters is not a string, particularly when they contain the binary data to form a float. If you want to see the values of those characters you need to do something like

      Code:
      printf("\n%d %d %d %d  Size:%d\n", 
             array1.str4[0],array1.str4[1],array1.str4[2],array1.str4[3],
             sizeof(array1.str4));

      Comment

      • rabidmonkey
        New Member
        • Feb 2007
        • 13

        #4
        Thanks Banfa, I feel soooo silly now :)

        So now I can convert those numbers to their binary equivalent and fill out a
        char array[32] with zero's and ones and I have a representation of the float in binary. Thanks for that but in reference to willakawill's answer am I right in saying I should not have a problem so long as my server app is writing float to memory only and the client is readonly? (its a data feed going to many clients)

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          I think what will is saying is that because a union is only allocated data for it's largest structure and all members of the union share that memory you can only use store discrete data for 1 member at a time.

          This does not effect you because the whole point of putting the data in the union in the float is so you can read it out as binary.

          When you are an array of 32 bytes and you are going to put 0 and 1 into it if you actually mean '0' and '1' rather than 0 and 1 in order to create a string then you need an array of 33 char so that you can put in the NULL terminator.

          Comment

          • rabidmonkey
            New Member
            • Feb 2007
            • 13

            #6
            Nice 1 thanks for your help guy I am back on track now :)

            Comment

            • willakawill
              Top Contributor
              • Oct 2006
              • 1646

              #7
              Banfa is absolutely correct here. I did not pay full attention to the question

              Comment

              Working...