1
I am using Ruby and have the string "AwX"
in hexadecimal. I can write to the screen using the following command:
puts "\x41\x77\x58"
But I want to do the reverse: I have a string "AwX"
, but I want to retrieve the hexadecimals of these characters (whether as array of strings, single string or other formats) and write the result on the screen (with the characters being represented in the hexadecimal notation, as being "0x41", "x41" or other formats). How this character conversion can be performed?
I tried to use the functions string.hex
and string.to_i
:
puts "A".hex # esperava "x41", mas foi retornado 10
puts "A".to_i(base=16) # esperava "x41", mas foi retornado 10
puts "A".to_i.to_s(16) # esperava "x41", mas foi retornado 0