writing uint16's to a binary file
Ken G. Brown
kbrown at tnc.ab.ca
Tue Mar 12 20:38:06 UTC 2002
Ok. That makes sense and that's what I understood.
I see now that the nextWordPut: was doing me in.
Now this is as it should be:
----
test12 _ ByteArray new: 8.
test12 unsignedShortAt: 1 put: 16r0102 bigEndian: true;
unsignedShortAt: 3 put: 16r0304 bigEndian: true.
outfile _ (StandardFileStream newFileNamed: 'outfile12.hws') binary.
outfile nextPutAll: test12.
outfile close.
get in file
01 02 03 04 00 00 00 00
And with BigEndian: false
02 01 04 03 00 00 00 00
----
However when I was doing with BigEndian: true
test12 do: [:ii | outfile nextWordPut: ii].
I was getting extra bytes manufactured:
00 01 00 02 00 03 00 04 00 00 00 00 00 00 00 00
Big clue! There were twice as many bytes in the file as in the array.
I believe I understand now.
Thx a lot everyone.
Ken
> -----Original Message-----
> From: squeak-dev-admin at lists.squeakfoundation.org
> [mailto:squeak-dev-admin at lists.squeakfoundation.org]On Behalf Of Ned
> Konz
> Sent: March 12, 2002 12:12 PM
> To: squeak-dev at lists.squeakfoundation.org
> Subject: Re: writing uint16's to a binary file
>
>
> On Tuesday 12 March 2002 10:49 am, Ken Brown wrote:
> > When I index into the ByteArray at 2, I'm having trouble
> understanding what
> > it means exactly.
>
> ByteArrays hold bytes, and are indexed starting at 1.
>
> so:
>
> b _ ByteArray new: 4.
> b at: 2 put: 1.
> a ByteArray(0 1 0 0)
>
> Simple.
>
> However, there are also some methods in there for storing
> bigger things than
> bytes -- on arbitrary byte boundaries.
>
> so:
>
> b _ ByteArray new: 8.
> b unsignedShortAt: 2 put: 16r0102 bigEndian: true.
> b
> a ByteArray(0 1 2 0 0 0 0 0)
>
> b _ ByteArray new: 8.
> b unsignedShortAt: 2 put: 16r0102 bigEndian: false.
> b a ByteArray(0 2 1 0 0 0 0 0)
>
> The "2" in the above is a _byte_ index into the array; we're
> storing a 2-byte
> thing there.
>
> --
> Ned Konz
> currently: Stanwood, WA
> email: ned at bike-nomad.com
> homepage: http://bike-nomad.com
More information about the Squeak-dev
mailing list
|