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

> Iki Baz Castillo 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
> -- 
> NP: Depeche Mode - It's No Good
> Jabber: sepp2k / jabber.org
> 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 Biedenharn		http://agileconsultingllc.com
Rob / AgileConsultingLLC.com