Skip to content

Instantly share code, notes, and snippets.

@south37
Last active August 29, 2015 13:56
Show Gist options
  • Save south37/9031242 to your computer and use it in GitHub Desktop.
Save south37/9031242 to your computer and use it in GitHub Desktop.
def my_hex(hex_str)
result = 0
hex_str.split('').reverse.each_with_index do |ch, i|
low, high = ['1'..'9'], ['a'..'f']
result += 16 ** i *
case ch
when *low then ch.to_i
when *high then ch.ord - 'a'.ord + 10
else 0
end
end
return result
end
s = '1d11e'
p my_hex(s)
# >> 119070
@south37
Copy link
Author

south37 commented Feb 16, 2014

これ見てたら何となく16進数変換したくなった

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment