# How to convert character to hexadecimal?

Hi, I want to convert some characteres to hexadecimal:

" " (space) => x20
"\t" (tab) => x09

and so. I'm trying using to_i(16) but don't get it working at all. Any

···

--
Iñaki Baz Castillo
<ibc@aliax.net>

Iñaki Baz Castillo wrote:

Hi, I want to convert some characteres to hexadecimal:

" " (space) => x20
"\t" (tab) => x09

and so.

"x%02x" % " "

=> "x20"

"x%02x" % "\t"

=> "x09"

I'm trying using to_i(16) but don't get it working at all.

String#to_i(b) converts a string representing a number in base b to an
integer. You want to convert an integer into a a string, so you'd use
Integer#to_s(16) except that that would return "9" (instead of "x09"
like you want) for "\t", so I used % (sprintf) above.

HTH,
Sebastian

···

--
NP: Depeche Mode - It's No Good
Jabber: sepp2k@jabber.org
ICQ: 205544826

Iñaki Baz Castillo wrote:

Hi, I want to convert some characteres to hexadecimal:

" " (space) => x20
"\t" (tab) => x09

and so. I'm trying using to_i(16) but don't get it working at all. Any

" ".to_s(16)

" ".sum.to_s(base=16)

I figured the last one out myself, don't know if it's reliable.

Regards,

Siep

···

--
Posted via http://www.ruby-forum.com/.

" ".unpack('H*')
=> "20"
"\t".unpack('H*')
=> "09"

Put an "x" or "0x" on the front if you wish:
"x"+(" ".unpack('H*'))

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On May 13, 2008, at 9:30 AM, Sebastian Hungerecker wrote:

Iñaki Baz Castillo wrote:

Hi, I want to convert some characteres to hexadecimal:

" " (space) => x20
"\t" (tab) => x09

and so.

"x%02x" % " "

=> "x20"

"x%02x" % "\t"

=> "x09"

I'm trying using to_i(16) but don't get it working at all.

String#to_i(b) converts a string representing a number in base b to an
integer. You want to convert an integer into a a string, so you'd use
Integer#to_s(16) except that that would return "9" (instead of "x09"
like you want) for "\t", so I used % (sprintf) above.

HTH,
Sebastian
--
NP: Depeche Mode - It's No Good
Jabber: sepp2k@jabber.org
ICQ: 205544826

Thanks, but I don't want to convert an integer into a String, but a

···

String#to_i(b) converts a string representing a number in base b to an
integer.

You want to convert an integer into a a string,

--
Iñaki Baz Castillo
<ibc@aliax.net>

Great !

···

2008/5/13, Rob Biedenharn <Rob@agileconsultingllc.com>:

" ".unpack('H*')
=> "20"
"\t".unpack('H*')
=> "09"

Put an "x" or "0x" on the front if you wish:
"x"+(" ".unpack('H*'))

--
Iñaki Baz Castillo
<ibc@aliax.net>

Iñaki Baz Castillo wrote:

Thanks, but I don't want to convert an integer into a String, but a

There's no such thing as a character or a hexedecimal value in ruby.
" " is a string.
" " is an integer(32)
"x20" is a string again.

···

--
NP: Depeche Mode - Walking In My Shoes [Seven Inch Mix]
Jabber: sepp2k@jabber.org
ICQ: 205544826

I think ' '.to_s.hex will produce the value and be a bit easier to
For example:
x = " \tX"
0.upto(x.size-1){|i| puts "x%02x"%x[i].to_s.hex}

There is a String.each_char, but it wouldn't work in the version of

Cheers

···

On May 13, 9:57 am, Iñaki Baz Castillo <i...@aliax.net> wrote:

2008/5/13, Rob Biedenharn <R...@agileconsultingllc.com>:

> " ".unpack('H*')
> => "20"
> "\t".unpack('H*')
> => "09"

> Put an "x" or "0x" on the front if you wish:
> "x"+(" ".unpack('H*'))

Great !

--
Iñaki Baz Castillo
<i...@aliax.net>

Chris Hulan wrote:

Great !

--
I�aki Baz Castillo
<i...@aliax.net>

I think ' '.to_s.hex will produce the value and be a bit easier to
For example:
x = " \tX"
0.upto(x.size-1){|i| puts "x%02x"%x[i].to_s.hex}

Besides being a candidate to win a code obfuscation competition, your
code doesn't give the proper results. For instance, if you start with
the string "abc":

x = "abc"
0.upto(x.size-1){|i| puts "x%02x"%x[i].to_s.hex}

--output:--
x97
x98
x99

Those are the ascii values written with an x in front. You don't
convert decimal numbers to hex by merely writing an "x" in front of
them. The hex value x97 is equal to 9*16 + 7*1 = 151 in decimal, which
is not the ascii code for an "a". The ascii code for an "a" is 97.

Your problems are a direct result of trying to cram all the code into
one line as well as using 'x' as a variable name, a character in the
string, a type specifier in the format string, and as the leading
charcacter in the output!

This works:

str = " \tC"

0.upto(str.length - 1) do |i|
ascii_code = str[i]
hex_str = ascii_code.to_s(16)

if hex_str.length == 1
hex_str = "0x0#{hex_str}"
else
hex_str = "0x#{hex_str}"
end

puts hex_str
end

--output:--
0x20
0x09
0x43

Or, you can use the cryptic format specifiers like this:

str = " \tC"

0.upto(str.length - 1) do |i|
ascii_code = str[i]
dec_str = ascii_code.to_s

puts "%#04x" % dec_str
end

--output:--
0x20
0x09
0x43

···

On May 13, 9:57 am, I�aki Baz Castillo <i...@aliax.net> wrote:

--
Posted via http://www.ruby-forum.com/.

7stud -- wrote:

Chris Hulan wrote:

Great !

--
I�aki Baz Castillo
<i...@aliax.net>

I think ' '.to_s.hex will produce the value and be a bit easier to
For example:
x = " \tX"
0.upto(x.size-1){|i| puts "x%02x"%x[i].to_s.hex}

Your problems are a direct result of ...

..and the hex method having a misleading name. Why anyone would call a
method 'hex' whose return value is a decimal number is hard to
comprehend.

···

On May 13, 9:57 am, I�aki Baz Castillo <i...@aliax.net> wrote:

--
Posted via http://www.ruby-forum.com/.

Thanks 7, i totally misrad what hex does...thats what I get for
goofing off at work 9^)

cheers

···

On May 13, 12:45 pm, 7stud -- <bbxx789_0...@yahoo.com> wrote:

Chris Hulan wrote:
> On May 13, 9:57 am, I�aki Baz Castillo <i...@aliax.net> wrote:
>> Great !

>> --
>> I�aki Baz Castillo
>> <i...@aliax.net>

> I think ' '.to_s.hex will produce the value and be a bit easier to
> For example:
> x = " \tX"
> 0.upto(x.size-1){|i| puts "x%02x"%x[i].to_s.hex}

Besides being a candidate to win a code obfuscation competition, your
code doesn't give the proper results. For instance, if you start with
the string "abc":

x = "abc"
0.upto(x.size-1){|i| puts "x%02x"%x[i].to_s.hex}

--output:--
x97
x98
x99

Those are the ascii values written with an x in front. You don't
convert decimal numbers to hex by merely writing an "x" in front of
them. The hex value x97 is equal to 9*16 + 7*1 = 151 in decimal, which
is not the ascii code for an "a". The ascii code for an "a" is 97.

Your problems are a direct result of trying to cram all the code into
one line as well as using 'x' as a variable name, a character in the
string, a type specifier in the format string, and as the leading
charcacter in the output!

This works:

str = " \tC"

0.upto(str.length - 1) do |i|
ascii_code = str[i]
hex_str = ascii_code.to_s(16)

if hex_str.length == 1
hex_str = "0x0#{hex_str}"
else
hex_str = "0x#{hex_str}"
end

puts hex_str
end

--output:--
0x20
0x09
0x43

Or, you can use the cryptic format specifiers like this:

str = " \tC"

0.upto(str.length - 1) do |i|
ascii_code = str[i]
dec_str = ascii_code.to_s

puts "%#04x" % dec_str
end

--output:--
0x20
0x09
0x43

--
Posted viahttp://www.ruby-forum.com/.

7stud -- wrote:

Why anyone would call a
method 'hex' whose return value is a decimal number is hard to
comprehend.

Agreed on the misleading name, but not on Integers being "decimal numbers".

···

--
NP: Depeche Mode - Strangelove
Jabber: sepp2k@jabber.org
ICQ: 205544826

Sebastian Hungerecker wrote:

7stud -- wrote:

Why anyone would call a
method 'hex' whose return value is a decimal number is hard to
comprehend.

Agreed on the misleading name, but not on Integers being "decimal
numbers".

You can only interpret the meaning of an Integer in reference to some
base. The return value of the hex method returns an Integer whose base
is 10. An integer whose reference is base 10 is called a "decimal
number".

···

--
Posted via http://www.ruby-forum.com/.

7stud -- wrote:

You can only interpret the meaning of an Integer in reference to some
base. The return value of the hex method returns an Integer whose base
is 10.

How does an Integer whose base is 10 differ from an Integer whose base is
something else? Or how do you know what base an Integer has?
Is 0xA a decimal number? No? So you'd probably say that 0xA is an Integer in
base 16 and 10 is an Integer in base 10, right? But 0xA and 10 are the same
object. 0xA.object_id == 10.object_id #=> true. How can that be if the base
is something inherent to the Integer (answer: it isn't).

An integer whose reference is base 10 is called a "decimal
number".

Integers don't have a base. Only string representations of Integers do.

···

--
NP: Depeche Mode - The Love Thieves
Jabber: sepp2k@jabber.org
ICQ: 205544826

Sebastian Hungerecker wrote:

7stud -- wrote:

You can only interpret the meaning of an Integer in reference to some
base. The return value of the hex method returns an Integer whose base
is 10.

How does an Integer whose base is 10 differ from an Integer whose base
is
something else? Or how do you know what base an Integer has?
Is 0xA a decimal number? No? So you'd probably say that 0xA is an
Integer in
base 16 and 10 is an Integer in base 10, right? But 0xA and 10 are the
same
object. 0xA.object_id == 10.object_id #=> true. How can that be if the
base
is something inherent to the Integer (answer: it isn't).

An integer whose reference is base 10 is called a "decimal
number".

Integers don't have a base. Only string representations of Integers do.

How ruby handles Integers has no bearing on whether a number is called a
"decimal number" or an "octal number" or a "hex number".

···

--
Posted via http://www.ruby-forum.com/.

7stud -- wrote:

How ruby handles Integers has no bearing on whether a number is called a
"decimal number" or an "octal number" or a "hex number".

Then I ask you again what does have a bearing on that?
x = 0xA
What kind of number is x?

···

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Sent: Tuesday, May 13, 2008 11:54 AM
To: ruby-talk ML
Subject: Re: How to convert character to hexadecimal?

7stud -- wrote:
> How ruby handles Integers has no bearing on whether a number is
called a
> "decimal number" or an "octal number" or a "hex number".

Then I ask you again what does have a bearing on that?
x = 0xA
What kind of number is x?

x.class.name will clearly show you that x in an instance of Integer. 0xA is just one of the integer literal notations. Others for the same number are 10, 012, 0b1010. Again, They are all "synonyms" for the same number:

0xA.object_id == 10.object_id == 012.object_id == 0b1010.object_id

Integer itself does not have a base, only human representation does. Both for literals and when you want a string or verbal representation of the number. So for your x one would say that it holds a number that may be represented as "10" in base 10, "A" in base 16, "12" in base 8, "1010" in base 2, "101" in base 3, "22" in base 4, and so on.

It is true no matter what language you use.

···

-----Original Message-----

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Then I ask you again what does have a bearing on that?
x = 0xA
What kind of number is x?

x is not a number--x is a variable. After the assignment, x refers to
an Integer object that was created from the Integer literal 0xA.

···

--
Posted via http://www.ruby-forum.com/.

Integer itself does not have a base, only human representation does.

Exactly my point.

···

--
NP: Depeche Mode - It's No Good
Jabber: sepp2k@jabber.org
ICQ: 205544826

7stud -- wrote:

After the assignment, x refers to
an Integer object that was created from the Integer literal 0xA.

Sure, but the Integer doesn't have any idea what kind of literal it was
created from. Thus making a distinction between decimal and hexadecimal
numbers as far as Integers are concerned is entirely pointless.
Also the integer that gets returned by the hex method was certainly not
created from any kind of literal, so calling that one a decimal number makes
even less sense.

···

--
NP: Depeche Mode - Martyr
Jabber: sepp2k@jabber.org
ICQ: 205544826