Created
December 16, 2012 17:54
-
-
Save midned/4310271 to your computer and use it in GitHub Desktop.
My way to convert decimal to hexadecimal and vice versa in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Fixnum | |
def to_hex | |
self.to_s 16 | |
end | |
end | |
class String | |
def to_i_from_hex | |
self.to_i 16 | |
end | |
alias :from_hex :to_i_from_hex | |
end | |
puts 30.to_hex # "1e" | |
puts '1e'.to_i_from_hex # 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment