Last active
December 20, 2015 01:09
-
-
Save afazio/6046581 to your computer and use it in GitHub Desktop.
Decode a hex string into a UTF-8 string. Try this hex string as an example: e299a5205765204d616b652054756d6d792048617070792120e299a5
This file contains hidden or 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
#!/usr/bin/env ruby | |
def usage | |
puts "usage: #{$0} <hex_string>" | |
exit 1 | |
end | |
def continuous_byte? (byte) | |
(0x80..0xBF).include?(byte) | |
end | |
def leading_byte? (byte) | |
!continuous_byte?(byte) | |
end | |
def decode_utf8_hex_string (hexstr) | |
# Convert string into array of integers, each representing 2 hex chars (1 byte) | |
bytes = hexstr.scan(/../).map { |unit| unit.hex } | |
# Create an array of integers representing individual UTF-8 chars | |
chars = bytes.inject([]) do |array, byte| | |
if leading_byte?(byte) | |
# This is a leading byte. Push it onto the array | |
array.push(byte) | |
else | |
# Continuous byte. Append to last leading or continuous byte | |
array[-1] << 8 | |
array[-1] += byte | |
end | |
array | |
end | |
# Return utf8 string | |
utf8 = chars.pack("U*") | |
end | |
def main (args) | |
usage unless args.length == 1 | |
puts decode_utf8_hex_string(args[0]) | |
end | |
main(ARGV.dup) if __FILE__ == $0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment