Hex to decimal?

T

Tim Hunter

Since nobody else has any Ruby to talk about today... :)

My gut feel is that this should be 2 or 3 lines of code but for some
reason I'm missing the trick.

I need a script that converts pairs of hex digits into their decimal
equivalents. Suppose the script accepts a single argument, a string
composed of hex digits, either upper- or lower-case, or both, possibly
with embedded blanks. For the sake of argument let's say that there's
always going to be at least 1 pair but never more than, oh, 20 pairs (or
as many as can easily be typed on a command line).

The script should remove any embedded blanks from the argument, then go
through the string left-to-right. For each pair of hex digits, display
the value of the pair as a 3-digit decimal number, separated with a
backslash.

So, for example,

ruby hex2dec.rb "f0 d7e2 28" => \240\215\226\040

Ideas?
 
J

Joel VanderWerf

Tim said:
ruby hex2dec.rb "f0 d7e2 28" => \240\215\226\040

def hex2dec hex
[hex.delete(" ")].pack("H*").unpack("C*").map {|n| "\\%03d" % n}.join
end

puts hex2dec("f0 d7e2 28") # ==> \240\215\226\040
puts hex2dec("d00de") # ==> \208\013\224
 
R

Rob Biedenharn

Since nobody else has any Ruby to talk about today... :)

My gut feel is that this should be 2 or 3 lines of code but for some
reason I'm missing the trick.

I need a script that converts pairs of hex digits into their decimal
equivalents. Suppose the script accepts a single argument, a string
composed of hex digits, either upper- or lower-case, or both, possibly
with embedded blanks. For the sake of argument let's say that there's
always going to be at least 1 pair but never more than, oh, 20 pairs
(or
as many as can easily be typed on a command line).

The script should remove any embedded blanks from the argument, then
go
through the string left-to-right. For each pair of hex digits, display
the value of the pair as a 3-digit decimal number, separated with a
backslash.

So, for example,

ruby hex2dec.rb "f0 d7e2 28" => \240\215\226\040

Ideas?


A bit different that Ara's, but this is a nearly method-for-word
translation:

class String
def h2d
[self.gsub(/\s+/,'')]. # remove embedded blanks
pack('H*'). # convert hex pairs to bytes
split(//). # array of single-byte strings
map{|c| "%03d"%[c[0]]}. # first(only) char as 3
decimal-digits
join('\\') # separated by backslash
end
end

Of course, that's not a script, but a method. In case this is
homework, that part is left as an exercise. ;-)

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
7

7stud --

Tim said:
Since nobody else has any Ruby to talk about today... :)

My gut feel is that this should be 2 or 3 lines of code but for some
reason I'm missing the trick.

I need a script that converts pairs of hex digits into their decimal
equivalents. Suppose the script accepts a single argument, a string
composed of hex digits, either upper- or lower-case, or both, possibly
with embedded blanks. For the sake of argument let's say that there's
always going to be at least 1 pair but never more than, oh, 20 pairs (or
as many as can easily be typed on a command line).

The script should remove any embedded blanks from the argument, then go
through the string left-to-right. For each pair of hex digits, display
the value of the pair as a 3-digit decimal number, separated with a
backslash.

So, for example,

ruby hex2dec.rb "f0 d7e2 28" => \240\215\226\040

Ideas?


hex_str = ARGV[0]
hex_str = hex_str.delete(" ")

results = []
stop_index = hex_str.length - 2

0.step(stop_index, 2) do |i|
decimal_num = hex_str[i,2].hex
results << sprintf("%03d", decimal_num)
end

out_str = results.join("\\")
puts out_str


$ ruby r1test.rb "F0 d7E2 28"
240\215\226\040
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top