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. Advertisements

  2. Aaron D. Gifford wrote in post #991376:
    I'd just do this:
    => "\253T\251\214\353\037\n\322"

    Add an extra '0' to the left of hex if it's an odd number of characters.
     
    Brian Candler, Apr 7, 2011
    #2
    1. Advertisements

  3. Oh, I forgot to do the reverse:
    => 12345678901234567890
     
    Brian Candler, Apr 7, 2011
    #3
  4. 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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.