Am I missing something about BER-compressed integer?

Discussion in 'Ruby' started by Francis Cianfrocca, Dec 8, 2006.

  1. All:

    The following:

    p [9999].pack("w")

    gives:

    "\316\017"

    but I expected to get:

    "\047\017"

    Notice the correct bit pattern for the upper octet (100111) appears in
    the octet generated by Ruby (11001110) but shifted and with an extra
    bit at the top. Is this a bug in pack("w"), or is my expectation
    wrong?

    Thanks,
    -f
     
    Francis Cianfrocca, Dec 8, 2006
    #1
    1. Advertising

  2. On 12/8/06, Francis Cianfrocca <> wrote:
    > All:
    >
    > The following:
    >
    > p [9999].pack("w")
    >
    > gives:
    >
    > "\316\017"
    >
    > but I expected to get:
    >
    > "\047\017"
    >
    > Notice the correct bit pattern for the upper octet (100111) appears in
    > the octet generated by Ruby (11001110) but shifted and with an extra
    > bit at the top. Is this a bug in pack("w"), or is my expectation
    > wrong?
    >
    > Thanks,
    > -f
    >
    >



    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.
     
    Francis Cianfrocca, Dec 8, 2006
    #2
    1. Advertising

  3. Francis Cianfrocca

    Edwin Fine Guest

    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/.
     
    Edwin Fine, Dec 9, 2006
    #3
  4. Francis Cianfrocca

    Edwin Fine Guest

    Francis Cianfrocca wrote:
    > On 12/9/06, Edwin Fine <> wrote:
    >>
    >> Ruby uses BER as specified by Perl. Excerpt from the perlpacktut page:
    >>

    > Thanks for this info. BER is not specified by Perl, of course, but
    > rather by
    > the ITU. The only normative language I've found on the "BER-compressed

    (snip)
    > Array#pack doesn't support this encoding as far as I know (I ended up
    > writing it by hand in the Net::BER module).


    What I meant was not that Perl specified the BER encoding. I just meant
    that Ruby's implementation behavior is based on the Perl pack behavior,
    which itself was based on a project at Casbah.org (which I could not
    locate). They explicitly say that it's not the BER from ASN.1. I don't
    know if that means it is or is not the one defined by ITU.

    --
    Posted via http://www.ruby-forum.com/.
     
    Edwin Fine, Dec 9, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ad
    Replies:
    6
    Views:
    3,303
  2. Joseph

    compressed suffix trie

    Joseph, Sep 22, 2004, in forum: Java
    Replies:
    1
    Views:
    547
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=
    Sep 22, 2004
  3. Replies:
    7
    Views:
    1,805
    espenskogen
    Jan 31, 2011
  4. Fiona

    Compressed HTML

    Fiona, Aug 26, 2004, in forum: HTML
    Replies:
    9
    Views:
    749
    Charles Angelich
    Aug 31, 2004
  5. Daniel Berger
    Replies:
    4
    Views:
    237
    Daniel Berger
    Oct 22, 2007
Loading...

Share This Page