遇到奇怪的 ruby 编码:
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
知道为什么这不是对称的!?为什么'm0'(decode64_strict)根本不起作用?输入字符串被填充为 base64 字母表中 4 个字符的倍数。这里是 14 x 6 位 = 84 位,即 10 1/2 8 位字节,即 11 个字节。但是解码后的字符串似乎放弃了最后一个 nybble?
我错过了一些明显的东西还是这是一个错误?解决方法?参看。http://www.ietf.org/rfc/rfc4648.txt