Reading an integer out of 1..3 bytes

hi gurus and nubys,
I’m working on a tiny dumb client for the Gnutella2/MP network, and I
have packets like this:

[length of payload_length field][other stuff][payload_length field]
^^header

[payload]
^^data

now: payload_length can be 1…3 byte, encoded in little endian
(same of my platform), is there a rubysh way of reading 1,2 or 3 bytes
on an integer from an IO?

PS
in the spec-draft I can read this example:

ReadBytes( (BYTE*)&nPacketLength, nLenLen );

the varname are obvious, but
I don’t know what kind of code is this and what it actually does…

hi gurus and nubys,
I’m working on a tiny dumb client for the Gnutella2/MP network, and I
have packets like this:

[length of payload_length field][other stuff][payload_length field]
^^header

[payload]
^^data

now: payload_length can be 1…3 byte, encoded in little endian
(same of my platform), is there a rubysh way of reading 1,2 or 3 bytes
on an integer from an IO?

PS
in the spec-draft I can read this example:

ReadBytes( (BYTE*)&nPacketLength, nLenLen );

Something like (untested):

l = packet[offset_to_payload_length_field].unpack(“c#{len_len}”)
payload_length = 0
l.each_with_index { |x, i| payload_length += x << i }

Guillaume.

···

On Tue, 2003-07-29 at 22:30, gabriele renzi wrote:

the varname are obvious, but
I don’t know what kind of code is this and what it actually does…

is there a rubysh way of reading 1,2 or 3 bytes
on an integer from an IO?

Like with String ‘unpack’?

str.unpack( format ) -> anArray
···
 Decodes str (which may contain binary data) according to the format
 string, returning an array of each value extracted.

possibly… but how actually use it?
I could read a string of 1 or 2 or 3 bytes, and use a case ? and what
format should I use? I suppose this is a reeeeally dumb question, but
I never really got unpack :frowning:

···

il 31 Jul 2003 21:21:00 GMT, Mike Hall mghall@enteract.com ha scritto::

is there a rubysh way of reading 1,2 or 3 bytes
on an integer from an IO?

Like with String ‘unpack’?

str.unpack( format ) → anArray

Decodes str (which may contain binary data) according to the format
string, returning an array of each value extracted.