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? Lets say, if we have a SmallInteger>>asByteArray then it should return an array of 4 bytes on 32 bit architecture and 8 bytes on 64 bits. Or maybe, as variant, can i convert somehow Smallinteger to Big one, without make it auto-converted back to SmallInteger? -- Best regards, Igor Stasenko AKA sig. |
On Feb 19, 2008, at 14:13 , 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? > Lets say, if we have a SmallInteger>>asByteArray > then it should return an array of 4 bytes on 32 bit architecture and 8 > bytes on 64 bits. > > Or maybe, as variant, can i convert somehow Smallinteger to Big one, > without make it auto-converted back to SmallInteger? What do you need this for? Why do you care if it is a SmallInteger? Doesn't #int32: do what you want? - Bert - |
On 19/02/2008, Bert Freudenberg <[hidden email]> wrote:
> > On Feb 19, 2008, at 14:13 , 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? > > Lets say, if we have a SmallInteger>>asByteArray > > then it should return an array of 4 bytes on 32 bit architecture and 8 > > bytes on 64 bits. > > > > Or maybe, as variant, can i convert somehow Smallinteger to Big one, > > without make it auto-converted back to SmallInteger? > > What do you need this for? Why do you care if it is a SmallInteger? > Doesn't #int32: do what you want? > they is small or big). I think i found solution, which makes my code platform independent. -- Best regards, Igor Stasenko AKA sig. |
In reply to this post by Igor Stasenko
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 > Lets say, if we have a SmallInteger>>asByteArray > then it should return an array of 4 bytes on 32 bit architecture and 8 > bytes on 64 bits. > > Or maybe, as variant, can i convert somehow Smallinteger to Big one, > without make it auto-converted back to SmallInteger? > > |
On 19/02/2008, Klaus D. Witzel <[hidden email]> 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. Since smalltalkers care little, how integers are stored, i implemented 2 methods for encoding and decoding integer values from bytearray. The only limitation of my method, is that it can't encode too large integers (with number of bytes > 127 to store it). encodeInt: anInteger | length bytes | length := anInteger digitLength. bytes := ByteArray new: length + 1. bytes at: 1 put: ( (length bitShift: 1) + (anInteger < 0 ifTrue: [1] ifFalse: [0]) ). 1 to: length do: [:i | bytes at: i+1 put: (anInteger digitAt: i) ]. ^ bytes ---- decodeInt: aBytes offset: index | neg result length | length := aBytes at: index. neg := (length bitAnd: 1) = 1. length := length bitShift: -1. result := Integer new: length neg: neg. 1 to: length do: [:i | result digitAt: i put: (aBytes at: index + i) ]. ^ result -- Best regards, Igor Stasenko AKA sig. |
>>>>> "Igor" == Igor Stasenko <[hidden email]> writes:
Igor> This is true, but not helps to write any code not related to VMMaker. Igor> It's a generic problem: Igor> suppose you want to pass ANY integer value to binary stream. Why not just use a storeString? -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <[hidden email]> <URL:http://www.stonehenge.com/merlyn/> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! |
In reply to this post by Igor Stasenko
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). /Klaus > Since smalltalkers care little, how integers are stored, i implemented > 2 methods for encoding and decoding integer values from bytearray. > > The only limitation of my method, is that it can't encode too large > integers (with number of bytes > 127 to store it). > > encodeInt: anInteger > | length bytes | > > length := anInteger digitLength. > bytes := ByteArray new: length + 1. > bytes at: 1 put: ( (length bitShift: 1) + (anInteger < 0 ifTrue: [1] > ifFalse: [0]) ). > > 1 to: length do: [:i | > bytes at: i+1 put: (anInteger digitAt: i) > ]. > > ^ bytes > ---- > decodeInt: aBytes offset: index > | neg result length | > > length := aBytes at: index. > neg := (length bitAnd: 1) = 1. > length := length bitShift: -1. > result := Integer new: length neg: neg. > 1 to: length do: [:i | result digitAt: i put: (aBytes at: index + i) ]. > > ^ result > |
On 19/02/2008, Klaus D. Witzel <[hidden email]> 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. |
In reply to this post by Igor Stasenko
On Tuesday 19 February 2008 8:12:07 pm Igor Stasenko wrote:
> 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. why not use binary-coded decimal (BCD)? BCD works fine across platforms for any size of integers. If size is an issue, you can always compress the whole stream. [1] http://en.wikipedia.org/wiki/Binary-coded_decimal Subbu |
Free forum by Nabble | Edit this page |