Converting an integer to a hexadecimal string in Ruby
Asked Answered
B

5

220

Is there a built in way to convert an integer in Ruby into its hexadecimal equivalent?

Something like the opposite of String#to_i:

"0A".to_i(16) #=>10

Like perhaps:

"0A".hex #=>10

I know how to roll my own, but it's probably more efficient to use a built in Ruby function.

Bloodshot answered 17/9, 2008 at 15:23 Comment(0)
H
347

You can give to_s a base other than 10:

10.to_s(16)  #=> "a"

Note that in ruby 2.4 FixNum and BigNum were unified in the Integer class. If you are using an older ruby check the documentation of FixNum#to_s and BigNum#to_s

Halfassed answered 17/9, 2008 at 15:27 Comment(5)
That's the answer I was looking for but it isn't documented on the linked page str.to_s => str is specified as not accepting parameters and has "Returns the receiver." as the only documentation, but it seems to workBloodshot
sorry about that copy paste mistake of course to_s on string doesn't take arguments but on Fixnum it does :)Halfassed
Ah, I was looking under Integer for a .to_s method and couldn't find one. I'll look under Fixnum next time as wellBloodshot
Make sure the original number is an instance of Fixnum, Float will throw an exception.Trio
warning - if you're then splitting the hex number into byte-size segments you'll have to check the length of the result, since it won't be padded with a 0Ciera
H
90

How about using %/sprintf:

i = 20
"%x" % i  #=> "14"
Holleran answered 17/9, 2008 at 15:25 Comment(7)
Thanks for showing this, I needed something that would get me a fixed length string prepended with '0'. ex: "%02X" % 10 #=> "0A"Alpenglow
And for the other ruby newbies out there: "#%02x%02x%02x" % [255, 0, 10] #=> "#ff000a" - took me a bit to figure out how to send several args.Eyeless
This is an extremely awesome snippet of Ruby!Fried
I am new to Ruby, but it seems to me that there is something very subtle going on here. Can someone explain? (+1 BTW)Camshaft
@TomD % is a String method that effectively provides a shorthand for sprintf formatting (they make the same internal calls). It's documented in the String class, see ruby-doc.org/core-1.9.3/String.html#method-i-25Bookseller
Less duplication: [255, 0, 10].map{|x| '%02x'%x}.joinSoulsearching
@AaronHinni: You can also do 10.to_s(16).rjust(2, "0")Somber
F
83

To summarize:

p 10.to_s(16) #=> "a"
p "%x" % 10 #=> "a"
p "%02X" % 10 #=> "0A"
p sprintf("%02X", 10) #=> "0A"
p "#%02X%02X%02X" % [255, 0, 10] #=> "#FF000A"
Fleuron answered 24/10, 2011 at 22:54 Comment(0)
T
14

Here's another approach:

sprintf("%02x", 10).upcase

see the documentation for sprintf here: http://www.ruby-doc.org/core/classes/Kernel.html#method-i-sprintf

Toadeater answered 29/3, 2011 at 15:27 Comment(1)
sprintf("%02X", 10) will be uppercase because of the upper case X. No need for the upcase method to be called. The specific section of the kernel is this: ruby-doc.org/core-1.9.3/Kernel.html#method-i-formatStillmann
R
6

Just in case you have a preference for how negative numbers are formatted:

p "%x" % -1   #=> "..f"
p -1.to_s(16) #=> "-1"
Resistive answered 24/4, 2014 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.