Re: Encoding of primitives for binary serialization
- From: Joshua Cranmer <Pidgeot18@xxxxxxxxxxxxxxx>
- Date: Wed, 22 Apr 2009 08:02:03 -0400
kb wrote:
It looks like reading/writing real data types (float/double) in binary
format, in a language and platform independent manner is pretty tough
to implement. (I've already implemented reading/writing for other data
types and it is working fine.)
Once you get around endian issues, there's no real problems to a binary format. I don't know of any major architectures that are not IEEE 754, for example; even so, writing a routine to convert a floating-point number from IEEE 754 to a native format would not be difficult.
But given that I have to stick to binary format, the other options is
to write float/double values as characters i.e. to first convert
(format) the value to string and then write the string in binary
format. Clearly this would mean some performance impact.
I'd also be concerned about precision. Converting decimals to and from string representations is liable to munge the lowest bits, assuming you even get a precise representation.
Does anybody have an idea as to how much impact will this have on the
performance? (writing float as byte vs converting the float value to a
string and then writing the string to the stream)
A single-precision floating point number will take up exactly four bytes in binary. A string representation would have 6-7 characters of significant figures, along with a likely decimal point. If the numbers are big enough, you'd also have a possible five more digits added (e-100, e.g.). So, at worst, your output string would be 13 characters long--thrice the size of the binary representation.
The performance of conversion is a different story. Java's conversion actually uses a miniature bignum library to get full precision on input and output, so I can't imagine that it's very fast, relatively speaking.
--
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
.
- Follow-Ups:
- References:
- Encoding of primitives for binary serialization
- From: kb
- Re: Encoding of primitives for binary serialization
- From: In the Middle of the Pack
- Re: Encoding of primitives for binary serialization
- From: Roedy Green
- Re: Encoding of primitives for binary serialization
- From: Arne Vajhøj
- Re: Encoding of primitives for binary serialization
- From: kb
- Encoding of primitives for binary serialization
- Prev by Date: Re: Difference between 'Read timeout' & 'Response timeout' & 'Connect timeout'
- Next by Date: Re: Encoding of primitives for binary serialization
- Previous by thread: Re: Encoding of primitives for binary serialization
- Next by thread: Re: Encoding of primitives for binary serialization
- Index(es):
Relevant Pages
|