![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 4
Rep Power: 0
![]() |
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... |
|
|
|
|
|
#2 |
|
Professional Programmer
|
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.
|
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Apr 2005
Posts: 4
Rep Power: 0
![]() |
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
)but how do I do something similar with a float? There is that "," that I really don't know what to do with ![]() |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
A float is just another binary number. It's just interpreted differently. Extend the principle you're currently using.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
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: c Syntax (Toggle Plain Text)
__________________
Robotics @ Maryland AUV Team - Software Lead |
|
|
|
|
|
#7 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
c Syntax (Toggle Plain Text)
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
sizeof char is always 1. It's defined in the standard.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,038
Rep Power: 5
![]() |
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;
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How do I take a square root? | Fall Back Son | C | 26 | Oct 23rd, 2006 12:24 PM |
| converting string to float | beginnerCCC | C | 22 | Oct 2nd, 2006 11:59 PM |
| PLEASE HELP Convert FLOAT to STRING | Pedro | C | 2 | May 23rd, 2005 10:55 AM |
| Two ints divided into a float. | Dygear | C++ | 2 | Apr 1st, 2005 5:42 PM |
| float precision and MFC... | Racine_ | C++ | 0 | Feb 5th, 2005 8:31 AM |