我有一个字符串数组
["0b01001010", "0b01001010", "0b01001010", "0b01001010"]
如何制作
[0b01001010, 0b01001010, 0b01001010, 0b01001010]
THX
答案 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]