[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to convert character to hexadecimal?

Iñaki Baz Castillo

5/13/2008 12:56:00 PM

SGksIEkgd2FudCB0byBjb252ZXJ0IHNvbWUgY2hhcmFjdGVyZXMgdG8gaGV4YWRlY2ltYWw6Cgog
ICIgIiAoc3BhY2UpICA9PiAgeDIwCiAgIlx0IiAodGFiKSAgID0+IHgwOQoKYW5kIHNvLiBJJ20g
dHJ5aW5nIHVzaW5nIHRvX2koMTYpIGJ1dCBkb24ndCBnZXQgaXQgd29ya2luZyBhdCBhbGwuIEFu
eQpoZWxwIHBsZWFzZT8KCi0tIApJw7Fha2kgQmF6IENhc3RpbGxvCjxpYmNAYWxpYXgubmV0Pgo=

19 Answers

Sebastian Hungerecker

5/13/2008 1:30:00 PM

0

I=C3=B1aki Baz Castillo wrote:
> 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: sepp2k@jabber.org
ICQ: 205544826

Rob Biedenharn

5/13/2008 1:54:00 PM

0


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

> I=F1aki Baz Castillo wrote:
>> 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: sepp2k@jabber.org
> 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://agileconsult...
Rob@AgileConsultingLLC.com


Iñaki Baz Castillo

5/13/2008 1:57:00 PM

0

MjAwOC81LzEzLCBTZWJhc3RpYW4gSHVuZ2VyZWNrZXIgPHNlcHAya0Bnb29nbGVtYWlsLmNvbT46
Cgo+IFN0cmluZyN0b19pKGIpIGNvbnZlcnRzIGEgc3RyaW5nIHJlcHJlc2VudGluZyBhIG51bWJl
ciBpbiBiYXNlIGIgdG8gYW4KPiAgaW50ZWdlci4KCj4gWW91IHdhbnQgdG8gY29udmVydCBhbiBp
bnRlZ2VyIGludG8gYSBhIHN0cmluZywKClRoYW5rcywgYnV0IEkgZG9uJ3Qgd2FudCB0byBjb252
ZXJ0IGFuIGludGVnZXIgaW50byBhIFN0cmluZywgYnV0IGEKY2hhcmFjdGVyIGludG8gSGV4YWRl
Y2ltYWwgdmFsdWUuCgotLSAKScOxYWtpIEJheiBDYXN0aWxsbwo8aWJjQGFsaWF4Lm5ldD4K

Iñaki Baz Castillo

5/13/2008 1:58:00 PM

0

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

Siep Korteling

5/13/2008 2:11:00 PM

0

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
> 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
--
Posted via http://www.ruby-....

Chris Hulan

5/13/2008 2:15:00 PM

0

On May 13, 9:57 am, Iñaki Baz Castillo <i...@aliax.net> wrote:
> 2008/5/13, Rob Biedenharn <R...@agileconsultingllc.com>:
>
> > " ".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 Castillo
> <i...@aliax.net>

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

Sebastian Hungerecker

5/13/2008 2:45:00 PM

0

I=C3=B1aki Baz Castillo 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.


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

7stud --

5/13/2008 4:46:00 PM

0

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 ' '[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 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 via http://www.ruby-....

7stud --

5/13/2008 5:04:00 PM

0

7stud -- 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 ' '[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.
--
Posted via http://www.ruby-....

Sebastian Hungerecker

5/13/2008 5:10:00 PM

0

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