Cisplatin Cisplatin - 17 days ago 5
Ruby Question

Ruby: How to convert a list of integers into a hexadecimal string?

I have an array of integers that range from

0
to
255
, each representing two hexadecimal digits. I want to convert this array into one hexadecimal string using Ruby. How would I do that?

Answer

I would do something like this:

array = [0, 128, 255]
array.map { |number| number.to_s(16).rjust(2, '0') }.join
#=> "0080ff"
Comments