bug with unpack('m') / pack('m') ?

Discussion in 'Ruby' started by tribalvibes, Aug 15, 2011.

  1. tribalvibes

    tribalvibes Guest

    Having a strange ruby encoding encounter:

    ruby-1.9.2-p180 :618 > s = "a8dnsjg8aiw8jq".ljust(16,'=')
    => "a8dnsjg8aiw8jq=="
    ruby-1.9.2-p180 :619 > s.size
    => 16

    ruby-1.9.2-p180 :620 > s.unpack('m0')
    ArgumentError: invalid base64
    from (irb):631:in `unpack'

    ruby-1.9.2-p180 :621 > s.unpack('m')
    => ["k\xC7g\xB28<j,<\x8E"]
    ruby-1.9.2-p180 :622 > s.unpack('m').first.size
    => 10

    ruby-1.9.2-p180 :623 > s.unpack('m').pack('m')
    => "a8dnsjg8aiw8jg==\n"
    ruby-1.9.2-p180 :624 > s.unpack('m').pack('m') == s
    => false

    Any idea why this is not symmetric!? And why is 'm0' (decode64_strict)
    not working at all? The input string is padded out to a multiple of 4
    characters in the base64 alphabet. Here it's 14 x 6 bits = 84 bits
    which is 10 1/2 8-bit bytes, i.e. 11 bytes. But the decoded string
    seems to drop the last nybble?

    Am I missing something obvious or is this a bug? Workaround? cf.
    http://www.ietf.org/rfc/rfc4648.txt

    http://stackoverflow.com/questions/7063737/ruby-base64-encode-decode-unpackm-troubles
    tribalvibes, Aug 15, 2011
    #1
    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. Stacy Mader
    Replies:
    4
    Views:
    786
    Ekkehard Morgenstern
    Nov 22, 2003
  2. tmp123
    Replies:
    5
    Views:
    865
    Tim Roberts
    May 4, 2007
  3. joe shoemaker

    Python pack and unpack question

    joe shoemaker, Jul 15, 2008, in forum: Python
    Replies:
    0
    Views:
    302
    joe shoemaker
    Jul 15, 2008
  4. Kero
    Replies:
    0
    Views:
    198
  5. Bill Atkins
    Replies:
    6
    Views:
    116
    Daniel Berger
    Jun 14, 2004
Loading...

Share This Page