binary array, packing and unpacking?
ned at bike-nomad.com
Fri Feb 14 16:48:13 UTC 2003
On Friday 14 February 2003 02:48 am, Ragnar Hojland Espinosa wrote:
> I'm making a little tcp/ip server using SocketStream.st and I'm
> having trouble finding a way to pack/unpack data from binary to
> strings and to shorts and longs, and to do ntohl and similar.
> So basically I have what you can see below, and I haven't figured
> out how to do:
> | data |
> ^ version, name, description. "???"
> Supposing version is a integer which should be converted to its
> int32 represetation, name is a string, and description is a string,
> how would you convert the stuff to a single array?
Look at ByteArray:
version := 16r01020304.
name := 'name'.
description := 'description'.
bytes := ByteArray new: 4 + name size + description size + 2.
pos := 1.
bytes unsignedLongAt: pos put: version bigEndian: true.
pos := 5.
bytes replaceFrom: pos to: pos + name size - 1 with: name asByteArray
pos := 5 + name size + 1.
bytes replaceFrom: pos to: pos + description size - 1 with:
description asByteArray startingAt: 1.
=> a ByteArray(1 2 3 4 110 97 109 101 0 100 101 115 99 114 105 112
116 105 111 110 0)
GPG key ID: BEEA7EFE
More information about the Squeak-dev