SmallInteger and machine word size

Igor Stasenko siguctua at gmail.com
Tue Feb 19 16:02:26 UTC 2008


On 19/02/2008, Klaus D. Witzel <klaus.witzel at cobss.com> wrote:
> On Tue, 19 Feb 2008 15:42:07 +0100, Igor Stasenko wrote:
>
> > On 19/02/2008, Klaus D. Witzel wrote:
> >> On Tue, 19 Feb 2008 14:13:09 +0100, Igor Stasenko wrote:
> >>
> >> > Hello,
> >> >
> >> > I want to use integer values to pass them over a binary data stream.
> >> > And for this i need to encode a smallinteger value into bytearray
> >> > (endianess not matters).
> >> > Is there any way how to do it in platform independent way?
> >>
> >> These are the requirements addressed by class InterpreterSimulator (and
> >> its superclass and subclasses) when it saves+restores its .image; you
> >> have
> >> the solutions in the VMMaker package.
> >>
> >> /Klaus
> >
> > This is true, but not helps to write any code not related to VMMaker.
> > It's a generic problem:
> >  suppose you want to pass ANY integer value to binary stream.
>
> Then forget about C-land and enter beautiful Squeak-land :) The next thing
> you might also want to put into your ByteArray is perhaps a String, so
> here's the data-independent solution:
>
>   | byteStream dataStream |
>   byteStream := (ByteArray new: 10) writeStream.
>   dataStream := DataStream on: byteStream.
>   dataStream nextPut: SmallInteger maxVal + 1; close.
>   "close not really needed for streams on arrays"
>   byteStream contents inspect.
>   dataStream := DataStream on: byteStream contents readStream.
>   dataStream next inspect
>
> No knowledge about digitLength, nor about digitAt* etc (which is
> *not*necessary* for you but for implementors of the Number hierarchy
> anyways).
>

It's good but not fast :) I'm writing a low-level code, so it's not a
case, where i need so high abstraction. I'm just wanted to be not
dependent from smallint sizes (be it 63 bits or 31 bits).


> /Klaus
>


-- 
Best regards,
Igor Stasenko AKA sig.



More information about the Squeak-dev mailing list