By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,411 Members | 999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,411 IT Pros & Developers. It's quick & easy.

Union as a converter

P: 13
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

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main()
  3. {
  5.   union un
  6.   {
  7.     float fl;
  8.     unsigned char str4[4];
  9.   }   array1;
  11.   unsigned char temp[4];
  12.   array1.fl=1256.667;
  13.   printf("\n%f   Size:%d\n",array1.fl,sizeof(array1.fl));
  14.   printf("\n%s  Size:%d\n",array1.str4,sizeof(array1.str4));
  15. }
Feb 22 '07 #1
Share this Question
Share on Google+
6 Replies

P: 1,646
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.
Feb 22 '07 #2

Expert Mod 5K+
P: 8,916
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

Expand|Select|Wrap|Line Numbers
  1. printf("\n%d %d %d %d  Size:%d\n", 
  2.        array1.str4[0],array1.str4[1],array1.str4[2],array1.str4[3],
  3.        sizeof(array1.str4));
Feb 22 '07 #3

P: 13
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)
Feb 22 '07 #4

Expert Mod 5K+
P: 8,916
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.
Feb 22 '07 #5

P: 13
Nice 1 thanks for your help guy I am back on track now :)
Feb 23 '07 #6

P: 1,646
Banfa is absolutely correct here. I did not pay full attention to the question
Feb 23 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.