I have a blob of data that contains Java double values written to
it. How would I parse those back into something Lisp understands? I'm
using SBCL 1.0.9.
S.
On 2007-08-31, Stefan Arentz <·············@gmail.com> wrote:
> I have a blob of data that contains Java double values written to
> it. How would I parse those back into something Lisp understands? I'm
> using SBCL 1.0.9.
Read its 32-bit halves as integers, then use
sb-kernel:make-double-float.
David Lichteblau <···········@lichteblau.com> writes:
> On 2007-08-31, Stefan Arentz <·············@gmail.com> wrote:
> > I have a blob of data that contains Java double values written to
> > it. How would I parse those back into something Lisp understands? I'm
> > using SBCL 1.0.9.
>
> Read its 32-bit halves as integers, then use
> sb-kernel:make-double-float.
Ah I think that was the final piece of the puzzle :-) Thanks!
S.
On Aug 31, 9:12 am, Stefan Arentz <·············@gmail.com> wrote:
> David Lichteblau <···········@lichteblau.com> writes:
> > On 2007-08-31, Stefan Arentz <·············@gmail.com> wrote:
> > > I have a blob of data that contains Java double values written to
> > > it. How would I parse those back into something Lisp understands? I'm
> > > using SBCL 1.0.9.
>
> > Read its 32-bit halves as integers, then use
> > sb-kernel:make-double-float.
>
> Ah I think that was the final piece of the puzzle :-) Thanks!
>
> S.
A more portable solution involves parsing out the various components
of the underlying representation into sign, exponent, and mantissa,
and use SCALE-FLOAT.
http://www.lisp.org/HyperSpec/Body/fun_decode-fl_decode-float.html#scale-float
This is straightforward if your representation's FLOAT-RADIX for the
floating-point type you are construction matches Java's choice (of
radix 2?). That's a pretty safe bet in these days of IEEE-compliant
floating point. Otherwise, you would have to convert the exponent, and
correct the mantissa for the "remainder", if any, of the exponent that
could not be converted.
On 2007-08-31, Stefan Arentz <·············@gmail.com> wrote:
>> sb-kernel:make-double-float.
> Ah I think that was the final piece of the puzzle :-) Thanks!
You could also implement it portably and use SCALE-FLOAT like
ieee-floats does it (see http://www.cliki.net/ieee-floats).
However, for full compatibility with Java you have to handle NaN and
infinity, which Common Lisp does not support portably. Hence
sb-kernel:make-double-float for a full solution.
P� Fri, 31 Aug 2007 13:12:09 +0200, skrev Stefan Arentz
<·············@gmail.com>:
>
> I have a blob of data that contains Java double values written to
> it. How would I parse those back into something Lisp understands? I'm
> using SBCL 1.0.9.
>
> S.
>
Have a look at
http://www.gigamonkeys.com/book/practical-parsing-binary-files.html