Skip to content

Instantly share code, notes, and snippets.

@midned
Created December 16, 2012 17:54
Show Gist options
  • Save midned/4310271 to your computer and use it in GitHub Desktop.
Save midned/4310271 to your computer and use it in GitHub Desktop.
My way to convert decimal to hexadecimal and vice versa in Ruby
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