From: Edwin Fine Date: 2006-12-09T16:20:18+09:00 Subject: Re: Am I missing something about BER-compressed integer? Francis Cianfrocca wrote: > On 12/8/06, Francis Cianfrocca wrote: >> but I expected to get: >> >> > > > I figured it out. There's no bug in Ruby. pack("w") produces the > integer representation that's occasionally used in BER to save space > when representing things like OIDs, not the "standard" BER > representation that requires a field-size prefix. Ruby uses BER as specified by Perl. Excerpt from the perlpacktut page: The pack code w has been added to support a portable binary data encoding scheme that goes way beyond simple integers. A BER (Binary Encoded Representation) compressed unsigned integer stores base 128 digits, most significant digit first, with as few digits as possible. Bit eight (the high bit) is set on each byte except the last. There is no size limit to BER encoding, but Perl won't go to extremes. --------- Applying the above rules: irb(main):045:0> printf("%08b%08b", 0b10000000 | (9999/128), 9999%128) 1100111000001111=> nil Ruby: irb(main):046:0> [9999].pack("w").each_byte { |b| printf("%08b", b) } 1100111000001111=> "\316\017" -- Posted via http://www.ruby-forum.com/.