writing uint16's to a binary file

Ken Brown ken.brown at zedisolutions.com
Tue Mar 12 18:49:05 UTC 2002


> But, now that I poke a bit, ByteArray seems *much*
> better. #shortAt:put:bigEndian: seems *exactly* what you want.
> 
> Cheers,
> Bijan Parsia.

Okay, I tried variations of this.
I'm having trouble understanding what's going on with the ByteArray. 
Further tests:

--------------------------
test5 _ ByteArray new: 8.
test5 shortAt: 2 put: 255 bigEndian: true;
      shortAt: 5 put: 32768 bigEndian: true.      
outfile _ (StandardFileStream newFileNamed: 'outfile5.hws') binary.
test5 do: [:ii | outfile nextWordPut: ii].
outfile close.

get from hex workshop on the file
00 00 00 00 00 FF 00 00 00 80 00 00 00 00 00 00
---
test6 _ ByteArray new: 8.
test6 shortAt: 2 put: 255 bigEndian: true;
      shortAt: 5 put: 32768 bigEndian: true.      
outfile _ (StandardFileStream newFileNamed: 'outfile6.hws') binary.
outfile nextPutAll: test6.
outfile close.

get
00 00 FF 00 80 00 00 00
-----
test7 _ ByteArray new: 8.
test7 shortAt: 2 put: 255 bigEndian: false;
      shortAt: 5 put: 32768 bigEndian: false.      
outfile _ (StandardFileStream newFileNamed: 'outfile7.hws') binary.
test7 do: [:ii | outfile nextWordPut: ii].
outfile close.

get
00 00 00 FF 00 00 00 00 00 00 00 80 00 00 00 00
----
test8 _ ByteArray new: 8.
test8 unsignedShortAt: 2 put: 255 bigEndian: true;
      unsignedShortAt: 5 put: 32768 bigEndian: true.      
outfile _ (StandardFileStream newFileNamed: 'outfile8.hws') binary.
test8 do: [:ii | outfile nextWordPut: ii].
outfile close.

get
00 00 00 00 00 FF 00 00 00 80 00 00 00 00 00 00
----
test9 _ ByteArray new: 8.
test9 unsignedShortAt: 2 put: 255 bigEndian: false;
      unsignedShortAt: 5 put: 32768 bigEndian: false.      
outfile _ (StandardFileStream newFileNamed: 'outfile9.hws') binary.
test9 do: [:ii | outfile nextWordPut: ii].
outfile close.

get
00 00 00 FF 00 00 00 00 00 00 00 80 00 00 00 00
----
test10 _ ByteArray new: 8.
test10 unsignedShortAt: 2 put: 255;
      unsignedShortAt: 5 put: 32768.      
outfile _ (StandardFileStream newFileNamed: 'outfile10.hws') binary.
test10 do: [:ii | outfile nextWordPut: ii].
outfile close.

get
00 00 00 FF 00 00 00 00 00 00 00 80 00 00 00 00

--------------------
The ByteArray methods are:
--
shortAt: index put: value bigEndian: aBool
	"Store a 16 bit integer quantity starting from the given byte index"
	self unsignedShortAt: index put: (value bitAnd: 16r7FFF) - (value
bitAnd: -16r8000) bigEndian: aBool.
	^value
--
unsignedShortAt: index put: value bigEndian: aBool
	"Store a 16 bit unsigned integer quantity starting from the given
byte index"
	aBool ifTrue:[
		self at: index put: (value bitShift: -8).
		self at: index+1 put: (value bitAnd: 255).
	] ifFalse:[
		self at: index+1 put: (value bitShift: -8).
		self at: index put: (value bitAnd: 255).
	].
	^value
--
unsignedShortAt: byteOffset put: value
	"Store a 16bit unsigned integer starting at the given byte offset"
	^self integerAt: byteOffset put: value size: 2 signed: false
--


When I index into the ByteArray at 2, I'm having trouble understanding what
it means exactly.
Any help appreciated. 
Thx.
Ken



More information about the Squeak-dev mailing list