Created
October 5, 2020 17:04
-
-
Save nick3499/33261cf9ebb676532afa216e223ccbb9 to your computer and use it in GitHub Desktop.
Convert Characters from UTF-8 to Binary, Hexadecimal, Octal, Base64
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
#! /bin/python3 | |
'''Given string, convert characters to hexadecimal, binary, hexadecimal and \ | |
base64 values. Script available under MIT license: opensource.org/licenses/MIT''' | |
from base64 import b64encode as b64 | |
def convert_chars(): | |
'''Convert characters to hexadecimal, binary, hexadecimal and base64 \ | |
values.''' | |
chr_keys = { | |
' ': '32', '!': '33', '"': '34', | |
'#': '35', '$': '36', '%': '37', '&': '38', "'": '39', | |
'(': '40', ')': '41', '*': '42', '+': '43', ',': '44', | |
'-': '45', '.': '46', '/': '47', '0': '48', '1': '49', | |
'2': '50', '3': '51', '4': '52', '5': '53', '6': '54', | |
'7': '55', '8': '56', '9': '57', ':': '58', ';': '59', | |
'<': '60', '=': '61', '>': '62', '?': '63', '@': '64', | |
'A': '65', 'B': '66', 'C': '67', 'D': '68', 'E': '69', | |
'F': '70', 'G': '71', 'H': '72', 'I': '73', 'J': '74', | |
'K': '75', 'L': '76', 'M': '77', 'N': '78', 'O': '79', | |
'P': '80', 'Q': '81', 'R': '82', 'S': '83', 'T': '84', | |
'U': '85', 'V': '86', 'W': '87', 'X': '88', 'Y': '89', | |
'Z': '90', '[': '91', '\\':'92', ']': '93', '^': '94', | |
'_': '95', '`': '96', 'a': '97', 'b': '98', 'c': '99', | |
'd':'100', 'e':'101', 'f':'102', 'g':'103', 'h':'104', | |
'i':'105', 'j':'106', 'k':'107', 'l':'108', 'm':'109', | |
'n':'110', 'o':'111', 'p':'112', 'q':'113', 'r':'114', | |
's':'115', 't':'116', 'u':'117', 'v':'118', 'w':'119', | |
'x':'120', 'y':'121', 'z':'122', '{':'123', '|':'124', | |
'}':'125', '~':'126' | |
} | |
string = input('Enter string: ') # string input | |
dec_nums = [] # decimal numbers | |
bin_nums = [] # binary numbers | |
hex_bytes = [] # hexadecimal bytes | |
oct_nums = [] # octal numbers | |
html_syms = [] # HTML symbol entities | |
base64_bytes = b64(string.encode('ascii')).decode('ascii') | |
for char in string: | |
dec_nums.append(chr_keys[char]) # append to dec_nums list | |
bin_nums.append(bin(int(chr_keys[char]))[2:].rjust(8, '0')) # append to bin_nums list | |
hex_bytes.append(hex(int(chr_keys[char]))[2:]) # append to hex_bytes list | |
oct_nums.append(oct(int(chr_keys[char]))[2:]) # append to oct_nums | |
html_syms.append(f'&#{chr_keys[char]};') # append to dec_nums list | |
print(f"\x1b[1;34mdecimal :\x1b[0m {' '.join(dec_nums)}") | |
print(f"\x1b[1;34mbinary :\x1b[0m {' '.join(bin_nums)}") | |
print(f"\x1b[1;34mhexadecimal :\x1b[0m {' '.join(hex_bytes)}") | |
print(f"\x1b[1;34moctal :\x1b[0m {' '.join(oct_nums)}") | |
print(f"\x1b[1;34mHTML symbols:\x1b[0m {' '.join(html_syms)}") | |
print(f"\x1b[1;34mbase64 :\x1b[0m {base64_bytes}") | |
if __name__ == '__main__': | |
convert_chars() # if stand-alone |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment