![]() |
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... |
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.
|
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.
|
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 ;) |
A float is just another binary number. It's just interpreted differently. Extend the principle you're currently using.
|
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: :
|
Quote:
:
|
sizeof char is always 1. It's defined in the standard.
|
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 |
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