I need to use the each_byte method on a given string and format it properly.
The string I am working with is stored in the variable the_string, and contains the following characters (whitespace and numbers are part of the string):
1. this string has leading space and too "MANY tabs and sPaCes betweenX"
The output I am looking for, when formatted properly, should look like this:
----------
C|Dec|Hex
----------
1| 49|0x31
.| 46|0x2E
| 32|0x20
| 32|0x20
| 32|0x20
| 32|0x20
| 32|0x20
| 32|0x20
| 32|0x20
| 32|0x20
| 32|0x20
| 32|0x20
| 32|0x20
| 32|0x20
| 32|0x20
| 32|0x20
| 32|0x20
t|116|0x74
h|104|0x68
i|105|0x69
s|115|0x73
| 32|0x20
s|115|0x73
t|116|0x74
r|114|0x72
i|105|0x69
n|110|0x6E
g|103|0x67
| 32|0x20
However, when I use
puts the_string.each_byte {|c| print c, ' ' }
I get a result that looks like this:
49 46 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 116 104 105 115 32 115 116 114 105 110 103 32 104 97 115 32 108 101 97 100 105 110 103 32 115 112 97 99 101 32 97 110 100 32 116 111 111 32 32 32 32 34 77 65 78 89 32 116 97 98 115 32 97 110 100 32 115 80 97 67 101 115 32 98 101 116 119 101
How can I format my result so that it prints the original character, the decimal value, and the hex value in a column format as shown above? I looked through the documentation and am not able to find details on how to use each_byte. Any help is appreciated!
each_byte? If this is an assignment, that's fine, but of course there are ways to construct the desired table without the use of that method. For example, as an exercise, try this:puts the_string.gsub(/./) { |c| ???? }.