Re: Encoding of primitives for binary serialization



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
.



Relevant Pages

  • printing bits ... the right way
    ... \param str The array of characters where the resulting string is to ... \param format A custom format string specific to converting ... render an integer type into a binary string. ...
    (comp.lang.c)
  • Re: Date Formatting in VB6
    ... That would mean your nicely formatted yyyy-mm-dd text is being converted ... Dim sDate As String, dDate As Date ... I thought about converting the string back to date for ... to put it in the proper format but I still get mm/dd/yyyy. ...
    (microsoft.public.vb.syntax)
  • Re: Date format detection
    ... Specifies the locale for which the date string is to be formatted. ... date format for this locale. ... the system default-date format for the specified locale. ... be enclosed within single quotation marks in the date format picture. ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: Code Guidelines
    ... the built-ins can't really be trusted to work with other code. ... Any code that uses String now has that change. ... `Format` as a noun would seem to be a constructor, ... As lengthy as that namespace is, it does not describe string formatting. ...
    (comp.lang.javascript)
  • Re: Date format detection
    ... > Specifies the locale for which the date string is to be formatted. ... > date format for this locale. ... > the system default-date format for the specified locale. ... > be enclosed within single quotation marks in the date format picture. ...
    (borland.public.delphi.thirdpartytools.general)