Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   splitting up a float (http://www.programmingforums.org/showthread.php?t=12635)

sDa Feb 21st, 2007 1:57 PM

splitting up a float
 
My problem is this:

I have a float that I want to send over... well never mind that's not important... but the thing is I can only send one byte at the time and as you know a float is 4 bytes so how do I do that w/o loosing precision?

oh and I know this isn't really a C problem but since I'm writing in C I placed it here...

andro Feb 21st, 2007 2:27 PM

Send a byte designated to mean a float is incoming, and then send the 4 bytes of the float? Whatever is receiving will have to know how to handle it.

DaWei Feb 21st, 2007 2:30 PM

Hook the bytes together on the other end. If the 'other end' is a different platform, you'll have to get the order right. If it can't deal with 4-byte entities, or it uses a different floating-point procedure, your goose is cooked.

sDa Feb 21st, 2007 2:33 PM

I think I will have to explain it a bit more... I am sending the float over an I2C bus and what you do to send it is you send 1byte at the time so for instance if I want to send a 16bit int I first send var&0x00ff then (var&ff00)>>8 (I think :S)

but how do I do something similar with a float? There is that "," that I really don't know what to do with ;)

DaWei Feb 21st, 2007 3:18 PM

A float is just another binary number. It's just interpreted differently. Extend the principle you're currently using.

Game_Ender Feb 21st, 2007 8:37 PM

And make sure your target device understands/uses the same floating point format as the other.

On another note the easier way to do this would be something like this (just a demo not real code:
:

  1.  
  2. /* bytes is an array of characters the is size long
  3.  * we assume that sizeof(char) == 1, but that is true on almost all platforms
  4.  */
  5. void send_over_i2c(char* bytes, int size)
  6. {
  7.     int i = 0;
  8.     for (i = 0; i < size; ++i)
  9.         i2c_send(char[i]);
  10. }
  11.  
  12. /* Example */
  13. float myval = 10.321;
  14.  
  15. /* Treat the float pointer as the beginning to an array of chars */
  16. send_over_i2c((char*)&myval, sizeof(char));


InfoGeek Feb 21st, 2007 11:40 PM

Quote:

:

  1. send_over_i2c((char*)&myval, sizeof(char));


I think you meant...
:

  1. send_over_i2c((char*)&myval, sizeof(float));


DaWei Feb 22nd, 2007 4:40 AM

sizeof char is always 1. It's defined in the standard.

lectricpharaoh Feb 22nd, 2007 1:19 PM

Assuming that floats are the same (size, byte order, binary representation) are the same for sender and receiver, you could use a union.
:

typedef union
{
  float f;
  char b[4];
} floatBytes;

This way, you stick a float in the union, and grab the bytes (well chars) out. If you're not familiar with unions, they're like structs, only all members start at the same offset (they share the memory, in other words), and the size is the size of the largest member, plus any padding (as opposed to structs, which are the sum of all members and any padding).

jim mcnamara Feb 22nd, 2007 4:30 PM

Over tcp the default is big-endian. Even Windows supports
htonl(unsigned long hostlong);
and
ntohl(unsigned long networklong);

Assuming your floats are IEEE (32bit), then you call these to correct endianess. ntohl on recv and htonl on send.


All times are GMT -5. The time now is 1:48 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC