Last active
May 13, 2021 21:49
-
-
Save sspreitzer/cd686f47946dfc50fe978fffc69faca2 to your computer and use it in GitHub Desktop.
ruby base16
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
# Ruby base16 encode/decode | |
# (c) Sascha Spreitzer, 2016 | |
# MIT license | |
def b16decode(what) | |
chars = '' | |
ret = '' | |
what.each_char do |c| | |
chars += c | |
if chars.size == 2 | |
ret += chars.to_i(16).chr | |
chars = '' | |
end | |
end | |
ret | |
end | |
def b16encode(what) | |
ret = '' | |
what.each_char do |c| | |
ch = c.ord.to_s(16) | |
if ch.size == 1 | |
ch = '0' + ch | |
end | |
ret += ch | |
end | |
ret.upcase | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment