Integer to Hex

Hi,
Is there an easy way to get from an Integer to a hex representation (i.e.
the opposite of String#hex)?
Regards,
Shaun.

Apparently, Shaun Smith recently wrote:

Hi,
Is there an easy way to get from an Integer to a hex representation
(i.e. the opposite of String#hex)?
Regards,
Shaun.

There are lots of ways to do it; here is one way:

x = 10
sprintf(“%x”, x) # → “a”

This is nice because you can get it in different forms:

sprintf(“0x%x”, x) # → “0xa”
sprintf(“0x%04x”, x) # → “0x000a”

Simple and easy to use… assuming you think sprintf is simple and easy to
use. :wink:

Is there an easy way to get from an Integer to a hex representation (i.e.
the opposite of String#hex)?
% irb

printf(“%x\n”, 10)
a
nil

···

On Fri, Nov 15, 2002 at 01:50:58AM +0900, Shaun Smith wrote:

_


[ Wojtek gminick Walczak ][ http://gminick.linuxsecurity.pl/ ]
[ gminick (at) hacker.pl ][ gminick (at) underground.org.pl ]

Hello Shaun,

Thursday, November 14, 2002, 7:50:58 PM, you wrote:

Is there an easy way to get from an Integer to a hex
representation (i.e. the opposite of String#hex)?

p ‘%X’ % 240

···


Best regards,
Bulat mailto:bulatz@integ.ru

Apparently, Shaun Smith recently wrote:

Hi,
Is there an easy way to get from an Integer to a hex representation
(i.e. the opposite of String#hex)?
Regards,
Shaun.

There are lots of ways to do it; here is one way:

x = 10
sprintf(“%x”, x) # → “a”

This is nice because you can get it in different forms:

sprintf(“0x%x”, x) # → “0xa”
sprintf(“0x%04x”, x) # → “0x000a”

I prefer this form:
i = 10
“%x” % i

and correspondingly

“0x%x” % i
“0x%04x” % i

This form is exact equivalent to sprintf, however look more Ruby-like :wink:

···

----- Original Message -----
From: “Wesley J. Landaker” wjl@icecavern.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, November 14, 2002 8:56 AM
Subject: Re: Integer to Hex

Simple and easy to use… assuming you think sprintf is simple and easy to
use. :wink: