From: Guillaume Marcais Date: 2003-07-30T23:50:51+09:00 Subject: Re: Reading an integer out of 1..3 bytes On Tue, 2003-07-29 at 22:30, gabriele renzi wrote: > 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. > > the varname are obvious, but > I don't know what kind of code is this and what it actually does.. > >