如何从ruby中的字符串中提取字节文字

时间:2014-09-15 05:13:08

标签: ruby

我有一个字符串数组

["0b01001010", "0b01001010", "0b01001010", "0b01001010"]

如何制作

[0b01001010, 0b01001010, 0b01001010, 0b01001010]

THX

2 个答案:

答案 0 :(得分:3)

您可以将序列映射到二进制文件:

["0b01001010", "0b01001010", "0b01001010", "0b01001010"].map{ |e| e.to_i(2) }
# => [74, 74, 74, 74]

要回映:

[74, 74, 74, 74].map{ |e| e.to_s(2) }
# => ["1001010", "1001010", "1001010", "1001010"]

答案 1 :(得分:0)

["0b01001010", "0b01001010", "0b01001010", "0b01001010"]
.map{|s| eval(s)}
# => [74, 74, 74, 74]

["0b01001010", "0b01001010", "0b01001010", "0b01001010"]
.map{|s| s[2..-1].to_i(2)}
#=> [74, 74, 74, 74]