From: "Gennady F. Bystritsky" Date: 2002-11-15T02:05:08+09:00 Subject: Re: Integer to Hex ----- Original Message ----- From: "Wesley J. Landaker" To: "ruby-talk ML" Sent: Thursday, November 14, 2002 8:56 AM Subject: Re: Integer to Hex > 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 ;-) > Simple and easy to use... assuming you think sprintf is simple and easy to > use. ;) > > > > >