Home » Community » Newbie corner » Writing Float big-endian on Windows
Writing Float big-endian on Windows [message #31355] |
Fri, 25 February 2011 02:04  |
RedDevil
Messages: 2 Registered: February 2011
|
Junior Member |
|
|
I am just starting to use U++ and find the experience very satisfying so far. I need to be able to read and write floats (SP) is big-endian format. I could not fine the equivalent to something like 'Put32be'. Any help would be appreciated - thanks.
|
|
|
Re: Writing Float big-endian on Windows [message #31356 is a reply to message #31355] |
Fri, 25 February 2011 07:19   |
|
RedDevil wrote on Fri, 25 February 2011 02:04 | I am just starting to use U++ and find the experience very satisfying so far. I need to be able to read and write floats (SP) is big-endian format. I could not fine the equivalent to something like 'Put32be'. Any help would be appreciated - thanks.
|
Hi RedDevil
You can use the Put32be for this as well. All you have to do is to (carefully) cast the number into integer. Something like this should work:
stream.Put32be(*((int*)&f)); //where f is your float
Note that you must cast pointers, not the float directly, otherwise the value would be converted (and the bits would change). Similar trick is possible when reading as well, just the other way around.
Best regards,
Honza
|
|
|
Re: Writing Float big-endian on Windows [message #31376 is a reply to message #31356] |
Sat, 26 February 2011 06:28   |
gprentice
Messages: 260 Registered: November 2005 Location: New Zealand
|
Experienced Member |
|
|
If you care about portability you shouldn't assume that int is 32 bits or that float is 32 bits. They don't have to be the same size.
Also, I don't know why stream provides big endian and little endian functions instead of choosing automatically but assuming that float is big endian isn't exactly portable either. If you use Get and Put I wonder why would you care about how it's stored?
Graeme
[Updated on: Sat, 26 February 2011 06:29] Report message to a moderator
|
|
|
Re: Writing Float big-endian on Windows [message #31377 is a reply to message #31355] |
Sat, 26 February 2011 07:27   |
|
Oups, gprentice is right about the int In U++ you should use int32 or some similar type where the length is guaranteed. In standard c++ there is int32_t defined somewhere.
As for to reason for using certain endianness, I can see at least two First, if you want to store data on one machine and read them on other, which has possible different architecture. Second, there is many standard file formats which have prescribed endianness (it is actually related to the first case), so that you have to obey it, even if you will always run it on the same machine, because otherwise other application wouldn't interpret the file correctly.
Honza
|
|
|
|
Goto Forum:
Current Time: Sat Apr 26 22:02:37 CEST 2025
Total time taken to generate the page: 0.02795 seconds
|