Skip to content

Instantly share code, notes, and snippets.

@dmcnulla
Last active March 26, 2016 18:23
Show Gist options
  • Save dmcnulla/749278e1465b79e2bb26 to your computer and use it in GitHub Desktop.
Save dmcnulla/749278e1465b79e2bb26 to your computer and use it in GitHub Desktop.
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
@dmcnulla
Copy link
Author

Converts a string of "hexidecimal" or "hex" to a sting of "binary". Whitespace passes through to the returned string.

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