Binary in an http request

Andreas Raab Andreas.Raab at gmx.de
Wed Jun 12 21:18:15 UTC 2002


> But if I want something else than a String ? Such as an 
> imageSegment :-)

Then just convert it to a String/ByteArray first. It's simple (and even
reasonably efficient):

#--- snip ---
ArrayedCollection>>convertWordsTo: aByteClass
	"Convert the receiver into an appropriate byte-like class.
	Make sure the result is represented in big-endian format."
	| src dst |
	src := Form new hackBits: self.
	Smalltalk isLittleEndian ifTrue:[src swapEndianess].
	dst := aByteClass new: self basicSize * 4.
	src displayOn: (Form new hackBits: dst).
	^dst

ArrayedCollection class>>convertWordsFrom: aByteThing
	"Convert aByteThing into a (word-indexed) instance of the
receiver.
	Read all data from aByteThing in big-endian format"
	| src dst |
	src := Form new hackBits: aByteThing.
	Smalltalk isLittleEndian ifTrue:[src swapEndianness].
	dst := self basicNew: aByteThing size // 4.
	src displayOn: (Form new hackBits: dst).
	^dst

Form>>swapEndianness
	"Swap from LSB to MSB pixels or vice versa"
	depth := 0 - depth.

#--- snap ---

(WordArray with: 1) convertWordsTo: ByteArray
	=>  a ByteArray(0 0 0 1)


FloatArray convertWordsFrom: 
	((FloatArray with: 42.0) convertWordsTo: String)
	=>  a FloatArray(42.0)

Ahhh - the powers of BitBlt :-)

Cheers,
  - Andreas




More information about the Squeak-dev mailing list