Write/Read 64bit standard double Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
spaceman
Earned a small fee
Earned a small fee
Posts: 23
Joined: Wed Oct 22, 2008 11:14 am

Write/Read 64bit standard double

Post by spaceman » Mon Nov 30, 2009 3:49 pm

Hello,

I need to write a double array to a binary file using standard 64 bit double format. I am aware of the patch that you are developing in order to allow for this (http://trac.wxwidgets.org/ticket/10625), but I am working with wxMac 2.8.9, and the patch is planned (as it seems) for version 2.9.1 or so.
My question now is: is there any way to write binary doubles with only 64 bit? I tried it using the wxFileOutputStream class instead of the wxDataOutputStream class, but without success.
The file that I am writing needs to be read by another (external) application that only "understands" 64bit double.

Do you have an idea? Any help would be greatly appreciated!
Thanks,

Joerg

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Mon Nov 30, 2009 5:40 pm

Hi,

first of all, just a small clarification : you are not talking to the authors of wxWidgets here, but to a community of wxWidgets users. So "we" don't plan what goes into wx ;)

I have not used wxDataOutputStream much, but maybe you could use Write64, with some low-level cas from double to wxUInt64
"Keyboard not detected. Press F1 to continue"
-- Windows

Debster
Knows some wx things
Knows some wx things
Posts: 32
Joined: Sat Aug 20, 2005 6:01 pm

Post by Debster » Mon Nov 30, 2009 10:47 pm

Hi,

don't understand your problem, what's with simply:

double d;
file.write(&d, sizeof(double));

spaceman
Earned a small fee
Earned a small fee
Posts: 23
Joined: Wed Oct 22, 2008 11:14 am

Post by spaceman » Sun Dec 06, 2009 10:49 pm

Well, thanks for that advise - I have only used wxDataStreams and wxFileStreams for IO, so I didn't think about that rather simple solution...

All the best,
Joerg

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Mon Dec 07, 2009 1:49 pm

Debster and Joerg,

be aware that writing the double to disk the way you are doing now, can be tricky if you go cross platorm or even between different architectures. There are a couple of things to consider.

1) The double binary compact form between mac and windows is not compatible, so reading it back on a mac will not give you the value back
2) Between windows / linux machines there can even be a little to big endian and vice versa problem which will mess up the byte order when reading

The most safe form is using an IEEE extended form for your double before writing it, and reading it back in that same form, that will ensure the written binary double is always the same. You can also convert it to a decimal ASCII representation and use strtof and the likes to convert it back

Quick lookup, not sure if it is exactly what is needed:
http://babbage.cs.qc.edu/courses/cs341/ ... ences.html

Just a word of caution that writing doubles as binary data to disk will not always work the way you want it to be.

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

Post Reply