Why is hex -> base64 so different from base64 -> hex using pack and unpack?

时间秒杀一切 提交于 2019-12-09 17:17:30

问题


I got this code working, which converts from hex to base64, and vice versa. I got to_base64 from another SO question, and I wrote to_hex with some guesswork and trial and error.

class String

  def to_base64
    [[self].pack("H*")].pack("m0")
  end

  def to_hex
    self.unpack("m0").first.unpack("H*").first
  end
end

But I don't really grok the pack and unpack methods, even after reading the docs. Specifically, I'm confused by the asymmetry between the two implementations. Conceptually, in both cases, we take a string encoded in some base (16 or 64), and we wish to convert it to another base. So why can't we implement to_hex like this:

def to_hex
  [[self].pack("m0")].pack("H*")
end

or to_base64 using unpack? Why does the base we chose completely change the method we need to use to accomplish conversions?


回答1:


to_hex is the exact inverse of to_base64:

to_base64

  1. put string in an array: [self]
  2. call pack with H*: [self].pack("H*")
  3. put string in an array: [[self].pack("H*")]
  4. call pack with m0: [[self].pack("H*")].pack("m0")

to_hex

  1. call unpack with m0: self.unpack("m0")
  2. extract string from array: self.unpack("m0").first
  3. call unpack with H*: self.unpack("m0").first.unpack("H*")
  4. extract string from array: self.unpack("m0").first.unpack("H*").first

That's how you undo operations, by applying the inverse operations:

a = 5
(a + 4) * 3
#=> 27

And the other way around:

a = 27
(a / 3) - 4
#=> 5

a.pack is the inverse of a.unpack and a.first is the inverse of [a]



来源:https://stackoverflow.com/questions/18923515/why-is-hex-base64-so-different-from-base64-hex-using-pack-and-unpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!