Converting an integer to a hexadecimal string in Ruby
Solution 1
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
Solution 2
i = 20
"%x" % i #=> "14"
Solution 3
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"
Solution 4
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
Solution 5
Just in case you have a preference for how negative numbers are formatted:
p "%x" % -1 #=> "..f"
p -1.to_s(16) #=> "-1"
Related videos on Youtube
Matt Haughton
A developer with lots of experience in C#, Java and FileMaker. Some experience in LiveCode, Ruby on Rails, XSLT, C++, javascript and lots of other stuff ... and always wanting to learn more
Updated on June 12, 2020Comments
-
Matt Haughton about 4 years
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.
-
Matt Haughton almost 16 yearsThat'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 work
-
Jean almost 16 yearssorry about that copy paste mistake of course to_s on string doesn't take arguments but on Fixnum it does :)
-
Matt Haughton almost 16 yearsAh, I was looking under Integer for a .to_s method and couldn't find one. I'll look under Fixnum next time as well
-
ANeves over 13 yearsAnd 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. -
BookOfGreg about 12 years
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-format -
OzBandit over 11 yearsThis is an extremely awesome snippet of Ruby!
-
681234 over 11 yearsI am new to Ruby, but it seems to me that there is something very subtle going on here. Can someone explain? (+1 BTW)
-
tardate over 11 years@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-25
-
Rok Kralj almost 11 yearsLess duplication:
[255, 0, 10].map{|x| '%02x'%x}.join
-
Hubro over 10 years@AaronHinni: You can also do
10.to_s(16).rjust(2, "0")
-
onetwopunch over 9 yearsThanks for the number of digit formatting. Forgot about that :)
-
Adon over 8 yearsMake sure the original number is an instance of Fixnum, Float will throw an exception.
-
Tim Kretschmer about 8 yearssaved my day. nice RGB conversion