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
help please?

Iñaki Baz C. wrote:

Hi, I want to convert some characteres to hexadecimal:

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

and so.

“x%02x” % " "[0]
=> “x20”

“x%02x” % “\t”[0]
=> “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

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

“x%02x” % “\t”[0]

HTH,
Sebastian

NP: Depeche Mode - It’s No Good
Jabber: [email protected]
ICQ: 205544826

" ".unpack(‘H*’)[0]
=> “20”
“\t”.unpack(‘H*’)[0]
=> “09”

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

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

2008/5/13, Sebastian H. [email protected]:

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,

Thanks, but I don’t want to convert an integer into a String, but a
character into Hexadecimal value.

2008/5/13, Rob B. [email protected]:

" ".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ñaki Baz C. 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
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

On May 13, 9:57 am, Iñaki Baz C. [email protected] wrote:

Great !


Iñaki Baz C.
[email protected]

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[i].to_s.hex}

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

Cheers

Iñaki Baz C. wrote:

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.

Chris H. wrote:

On May 13, 9:57 am, I�aki Baz C. [email protected] wrote:

Great !


I�aki Baz C.
[email protected]

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[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 916 + 71 = 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

7stud – wrote:

Chris H. wrote:

On May 13, 9:57 am, I�aki Baz C. [email protected] wrote:

Great !


I�aki Baz C.
[email protected]

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

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

Sebastian H. 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”.

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.

Sebastian H. 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”.

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?

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.

Gennady.

Gennady B. wrote:

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

Exactly my point.

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.

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 B.'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.

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 B.'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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs