Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Writing Float big-endian on Windows
Writing Float big-endian on Windows [message #31355] Fri, 25 February 2011 02:04 Go to next message
RedDevil is currently offline  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 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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 Go to previous messageGo to next message
gprentice is currently offline  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 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Oups, gprentice is right about the int Smile 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 Smile 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
Re: Writing Float big-endian on Windows [message #31445 is a reply to message #31355] Thu, 03 March 2011 00:57 Go to previous message
RedDevil is currently offline  RedDevil
Messages: 2
Registered: February 2011
Junior Member
Thanks for the replies guys. As Honza indicated I am writing to a format that is defined to be BigEndian, but doing it on a Intel PC.
Previous Topic: TIme and Idle
Next Topic: Some code to add an icon in statusbar
Goto Forum:
  


Current Time: Sun May 05 09:30:20 CEST 2024

Total time taken to generate the page: 0.02346 seconds