How can I convert "1234567890" to {0x12, 0x34, 0x56, 0x78, 0x90} in Ruby?
A:
(0..4).map { |x| "0x%X" % (("1234567890".to_i(16) >> 8 * x) & 255) }.reverse
Kevin Peterson
2009-05-14 08:25:48
+1
A:
If you have a string containing numbers and you want to scan each as a numeric hex byte, I think this is what you want:
"1234567890".scan(/\d\d/).map {|num| Integer("0x#{num}")}
Chuck
2009-05-14 08:27:50
I'd have done: "1234567890".scan(/\d\d/).map {|i| i.to_i(16) }
Greg
2010-03-23 09:05:01
File.open('output.txt', 'w+') {|f| f.write(IO.read('input.txt').to_a.pack('H*')) }
neoneye
2010-06-24 14:47:33