[Glass] How to properly write bytes objects into binary streams?
Martin McClure via Glass
glass at lists.gemtalksystems.com
Wed Sep 3 13:53:25 PDT 2014
On 09/03/2014 01:22 PM, Mariano Martinez Peck wrote:
>
>
> #_basicAt: and #_basicAt:put: do work to access the bytes of large
> integers (and I think all byte objects).
>
>
> mmmmm I cannot access those in SmallDouble. I need to do a _asFloat.
> Maybe because it is "special" (immediate object?).
Right. SmallDoubles are not byte objects, they're immediate.
>
> Anyway, this doesn't work:
>
> | byte1 byte2 |
> byte1 := (1.1 _asFloat _basicAt: 1).
> byte2 := (1.1 _asFloat _basicAt: 2 ).
> (Float basicNew) _basicAt: 1 put: byte1; _basicAt: 2 put: byte2 ; yourself
>
> _basicAt: is answering strange numbers I think.
It should work. I think the problem is that you ended up with a two-byte
Float, and Floats are eight bytes.
Try this:
| sourceFloat destFloat |
sourceFloat := 1.1.
sourceFloat := sourceFloat _asFloat. "Must convert possible SmallDouble
to full Float."
destFloat := Float basicNew: 8.
1 to: 8 do: [:i | destFloat _basicAt: i put: (sourceFloat basicAt: i)].
^destFloat + 0.0. "Add 0 to possibly convert to SmallDouble."
>
> The trick is to create an instance of these class classes. For
> LargeInteger, for instance, this is currently quite restricted. The
> best I've come up with so far is to create one the correct size like
> this:
>
> | size int1 int2|
> size := <size in bytes you want>
> int1 := (2 raisedToInteger: (size - 4 * 8)) - 1.
> "int1 is the right size, but immutable"
> int2 := int1 copy.
> "int2 is mutable, and can be modified via #_basicAt:put:"
I'd missed Dale's previous message about #_new:neg:, which is a better
way than what I wrote above.
>
>
> OK, I see. THe way I found for Integers and LargeIntegers is to do a
> #asByteArray when serializing and #asInteger when materializing and use
> a regular #nextPutAll.
You're using the printString of the large integer to represent it,
rather than the actual bytes, then?
Regards,
-Martin
More information about the Glass
mailing list