Last active
March 26, 2016 18:23
-
-
Save dmcnulla/749278e1465b79e2bb26 to your computer and use it in GitHub Desktop.
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
MAP = { | |
'0' => '0000', | |
'1' => '0001', | |
'2' => '0010', | |
'3' => '0011', | |
'4' => '0100', | |
'5' => '0101', | |
'6' => '0110', | |
'7' => '0111', | |
'8' => '1000', | |
'9' => '1001', | |
'a' => '1010', | |
'b' => '1011', | |
'c' => '1100', | |
'd' => '1101', | |
'e' => '1110', | |
'f' => '1111' | |
} | |
def hex2bin(hex_string) | |
result = '' | |
hex_string.chars.each{ |c| | |
c = hex2bin_char(c) unless c.match /\s/ | |
result += c | |
} | |
result | |
end | |
def hex2bin_char(char) | |
MAP[char.downcase] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Converts a string of "hexidecimal" or "hex" to a sting of "binary". Whitespace passes through to the returned string.