在rails中将字节转换为字符串

时间:2019-06-11 01:26:26

标签: ruby encoding base64 byte

如何将字节数据转换为字符串,以便我对其执行base64解码,然后zlib将其解压缩。

示例:data = b'eJzLSM3JyQcABiwCFQ=='

Zlib::Inflate.inflate(Base64.decode64(bin_to_hex(data)))

def bin_to_hex(s)
s.unpack('C*').first
end

我要"\xE2" from ASCII-8BIT to UTF-8也要undefined method解包'`

1 个答案:

答案 0 :(得分:2)

您使事情变得过于复杂。我不知道数据文字中领先的b应该是什么意思,但这会起作用:

require 'base64'
Zlib::Inflate.inflate Base64.decode64('eJzLSM3JyQcABiwCFQ==')
#⇒ "hello"