Packing and unpacking unsigned integers of arbitrary size as binarystrings

Discussion in 'Ruby' started by Aaron D. Gifford, Apr 7, 2011.

  1. I see there's a 'w' option to pack for packing arbitrary sized
    unsigned integers in BER compressed format. However I need to
    pack/unpack binary strings as simple binary byte strings.

    The results of my own needs have been gemmified as the bignumpack gem.
    See https://github.com/astounding/bignumpack/blob/master/lib/bignumpack.rb

    Are there alternatives that may be more efficient, or if I've missed
    something obvious.

    I resorted to splitting the binary string into 64-bit sized chunks and
    using the 'Q' packing option (except where I couldn't determine
    endianness, where I resorted to using the 32-bit network-order 'N'
    packing option).

    While I tried to be endian-architecture friendly, I don't have a
    big-endian box with Ruby on it handy to test and would appreciate it
    if anyone on a big-endian box would let me know if the tests fail.

    Aaron out.
     
    Aaron D. Gifford, Apr 7, 2011
    #1
    1. Advertising

  2. Re: Packing and unpacking unsigned integers of arbitrary size asbinary strings

    Aaron D. Gifford wrote in post #991376:
    > Are there alternatives that may be more efficient, or if I've missed
    > something obvious.


    I'd just do this:

    >> num = 12345678901234567890

    => 12345678901234567890
    >> hex = num.to_s(16)

    => "ab54a98ceb1f0ad2"
    >> hex.size

    => 16
    >> [hex].pack("H*")

    => "\253T\251\214\353\037\n\322"

    Add an extra '0' to the left of hex if it's an odd number of characters.

    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Apr 7, 2011
    #2
    1. Advertising

  3. Re: Packing and unpacking unsigned integers of arbitrary size asbinary strings

    Oh, I forgot to do the reverse:

    >> bin = "\253T\251\214\353\037\n\322"

    => "\253T\251\214\353\037\n\322"
    >> bin.unpack("H*").first.to_i(16)

    => 12345678901234567890

    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Apr 7, 2011
    #3
  4. Re: Packing and unpacking unsigned integers of arbitrary size asbinary strings

    Nice, using the conversion via hex string is faster for
    number-to-binary-string conversion, but slower in string-to-number
    conversion on my system. I think I'll update and use it for
    number-to-string.

    Thanks!

    Aaron out.
     
    Aaron D. Gifford, Apr 7, 2011
    #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. Victor S. Miller

    Packing and unpacking bitvectors

    Victor S. Miller, Oct 9, 2003, in forum: Python
    Replies:
    0
    Views:
    305
    Victor S. Miller
    Oct 9, 2003
  2. Eelco
    Replies:
    121
    Views:
    1,814
    Eelco
    Jan 3, 2012
  3. Lucas L.
    Replies:
    12
    Views:
    205
    ara.t.howard
    Jun 16, 2008
  4. Julie

    Packing/unpacking

    Julie, Sep 22, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    111
    Chris Mattern
    Sep 22, 2003
  5. Shashank R Khanvilkar

    Packing/Unpacking bit fields from a byte.

    Shashank R Khanvilkar, Sep 25, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    439
    Anno Siegel
    Sep 26, 2005
Loading...

Share This Page