Re: encoding a float
On Mar 27, 8:59 pm, "hurcan solter" <hsol...@gmail.com> wrote:
On Mar 27, 7:43 pm, Ulrich Eckhardt <eckha...@satorlaser.com> wrote:
It does work:
// serialize
vec.resize(sizeof deneme);
memcpy( &vec.front(), &deneme, sizeof deneme);
// deserialize
assert(vec.size() >= sizeof deneme);
memcpy( &deneme2, &vec.front(), sizeof deneme);
HOWEVER: you still have a big problem there and that is that the layout of
floating-point values in memory is not portable between different > >
architectures. Therefore, I'd suggest you use a text-based format
which > >
makes it much easier to transfer the data between different machines.
thanks for the answer and it works. thing is i am not at liberty to
change the format
of layout. it must be BigEndian 32-bit IEEE normalized single-
precision format
(this is what i understand from memory layout)
It's not memory layout, but the line format, which is important.
which happens to be same on my platform,except the endiannes which i
deal with
before serializing it. so as long as the other side know how to
convert it
back to its native data type there should'nt be any problems right?
The basic rule is that there is a defined line format, to which
everyone has to adopt. Your problem, if you're writing, is to
convert to that format; you have to suppose that the other side
can convert from it. (If you're reading, of course, the problem
is the reverse.)
Given that, you have to consider how portable the code should
be. It's possible to write totally portable code to generate
32-bit big endian IEEE format. It's also a bit of work, and if
you know in advance that you'll never have to port to a machine
that doesn't use IEEE format internally, it's a lot easier to
play tricks with reinterpret_cast, to view the floating point as
a 32 bit unsigned integer, and output that in the desired order
(basically, what you did originally).
Whether this is an acceptable compromize depends a lot on
circumstances. IEEE is pretty much universal on desktop
computers and mainstream Unix machines; it is *not* widely used
on mainframes, and I don't know the situation on embedded
processors. Whether it's reasonable to limit your code to
desktop computers and mainstream Unix machines or not depends
very much on the context in which you find yourself; there is no
one right answer.
If you do go with the simpler solution, you might want to add
something like:
#if !defined(__STDC_IEC_559__)
#error This program requires IEEE floating point
#endif
at the head of your files. That way, anyone who tries to port
to a non-IEEE environment will get an error. (You'll almost
certainly have to and in some other conditions; not all
compilers which support IEEE define this macro, despite C99.)
--
James Kanze (GABI Software) mailto:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]