How to convert character to hexadecimal?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

SGksIEkgd2FudCB0byBjb252ZXJ0IHNvbWUgY2hhcmFjdGVyZXMgdG8gaGV4YWRlY2ltYWw6Cgog
ICIgIiAoc3BhY2UpICA9PiAgeDIwCiAgIlx0IiAodGFiKSAgID0+IHgwOQoKYW5kIHNvLiBJJ20g
dHJ5aW5nIHVzaW5nIHRvX2koMTYpIGJ1dCBkb24ndCBnZXQgaXQgd29ya2luZyBhdCBhbGwuIEFu
eQpoZWxwIHBsZWFzZT8KCi0tIApJw7Fha2kgQmF6IENhc3RpbGxvCjxpYmNAYWxpYXgubmV0Pgo=
 
S

Sebastian Hungerecker

I=C3=B1aki Baz Castillo said:
Hi, I want to convert some characteres to hexadecimal:

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

and so.
"x%02x" % " "[0] =3D> "x20"
"x%02x" % "\t"[0]
=3D> "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=20
integer. You want to convert an integer into a a string, so you'd use=20
Integer#to_s(16) except that that would return "9" (instead of "x09"
like you want) for "\t", so I used % (sprintf) above.
Any help please?

HTH,
Sebastian
=2D-=20
NP: Depeche Mode - It's No Good
Jabber: (e-mail address removed)
ICQ: 205544826
 
R

Rob Biedenharn

I=F1aki Baz Castillo said:
Hi, I want to convert some characteres to hexadecimal:

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

and so.
"x%02x" % " "[0] =3D> "x20"
"x%02x" % "\t"[0]
=3D> "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.
Any help please?

HTH,
Sebastian
--=20
NP: Depeche Mode - It's No Good
Jabber: (e-mail address removed)
ICQ: 205544826


" ".unpack('H*')[0]
=3D> "20"
"\t".unpack('H*')[0]
=3D> "09"

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

-Rob

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

Iñaki Baz Castillo

MjAwOC81LzEzLCBTZWJhc3RpYW4gSHVuZ2VyZWNrZXIgPHNlcHAya0Bnb29nbGVtYWlsLmNvbT46
Cgo+IFN0cmluZyN0b19pKGIpIGNvbnZlcnRzIGEgc3RyaW5nIHJlcHJlc2VudGluZyBhIG51bWJl
ciBpbiBiYXNlIGIgdG8gYW4KPiAgaW50ZWdlci4KCj4gWW91IHdhbnQgdG8gY29udmVydCBhbiBp
bnRlZ2VyIGludG8gYSBhIHN0cmluZywKClRoYW5rcywgYnV0IEkgZG9uJ3Qgd2FudCB0byBjb252
ZXJ0IGFuIGludGVnZXIgaW50byBhIFN0cmluZywgYnV0IGEKY2hhcmFjdGVyIGludG8gSGV4YWRl
Y2ltYWwgdmFsdWUuCgotLSAKScOxYWtpIEJheiBDYXN0aWxsbwo8aWJjQGFsaWF4Lm5ldD4K
 
I

Iñaki Baz Castillo

MjAwOC81LzEzLCBSb2IgQmllZGVuaGFybiA8Um9iQGFnaWxlY29uc3VsdGluZ2xsYy5jb20+OgoK
PiAgIiAiLnVucGFjaygnSConKVswXQo+ICA9PiAiMjAiCj4gICJcdCIudW5wYWNrKCdIKicpWzBd
Cj4gID0+ICIwOSIKPgo+ICBQdXQgYW4gIngiIG9yICIweCIgb24gdGhlIGZyb250IGlmIHlvdSB3
aXNoOgo+ICAieCIrKCIgIi51bnBhY2soJ0gqJylbMF0pCgoKR3JlYXQgIQoKCi0tIApJw7Fha2kg
QmF6IENhc3RpbGxvCjxpYmNAYWxpYXgubmV0Pgo=
 
S

Siep Korteling

Iñaki Baz Castillo said:
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
help please?

" "[0].to_s(16)

" ".sum.to_s(base=16)

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

Regards,

Siep
 
C

Chris Hulan

2008/5/13 said:
" ".unpack('H*')[0]
=> "20"
"\t".unpack('H*')[0]
=> "09"
Put an "x" or "0x" on the front if you wish:
"x"+(" ".unpack('H*')[0])

Great !

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

There is a String.each_char, but it wouldn't work in the version of
ruby i have access to...

Cheers
 
S

Sebastian Hungerecker

I=C3=B1aki Baz Castillo said:
Thanks, but I don't want to convert an integer into a String, but a
character into Hexadecimal value.

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


=2D-=20
NP: Depeche Mode - Walking In My Shoes [Seven Inch Mix]
Jabber: (e-mail address removed)
ICQ: 205544826
 
7

7stud --

Chris said:

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

puts "%#04x" % dec_str
end

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

7stud --

7stud said:
Chris said:

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

Sebastian Hungerecker

7stud said:
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".
 
7

7stud --

Sebastian said:
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".
 
S

Sebastian Hungerecker

7stud said:
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.
 
7

7stud --

Sebastian said:
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).



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".
 
S

Sebastian Hungerecker

7stud said:
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?
 
7

7stud --

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

To add a little to Gennady Bystritsky's answer:

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.
 
S

Sebastian Hungerecker

7stud said:
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.
 
S

Simon Krahnke

* 7stud -- said:
To add a little to Gennady Bystritsky's answer:

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.

And an integer literal is a string representation of an Integer. And
these string representations do have a base.

mfg, simon .... l
 
C

Chris Hulan

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

puts "%#04x" % dec_str
end

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

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

cheers
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top