-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.py
128 lines (103 loc) · 3.93 KB
/
converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def is_valid_unicode(unicode):
valid_chars = set('0123456789ABCDEF')
min = 0x0000
max = 0x10FFFF
# check if value has valid characters and length
if (all(char.upper() in valid_chars for char in unicode)):
decimal_value = int(unicode, 16)
if (decimal_value >= min) and (decimal_value <= max):
return True
return False
def convert_to_utf8(unicode):
if 0x00 <= int(unicode, 16) <= 0x7F:
unicode = unicode[-2:]
return unicode.zfill(2)
bin_unicode = bin(int(unicode, 16))
num_of_bits = len(bin_unicode) - 2
str_unicode = str(bin_unicode)
str_unicode = str_unicode[2:]
if (num_of_bits > 16):
start = "11110"
bytes_req = 4
str_unicode = str_unicode.zfill(21)
elif (num_of_bits > 11):
start = "1110"
bytes_req = 3
str_unicode = str_unicode.zfill(16)
else:
start = "110"
bytes_req = 2
str_unicode = str_unicode.zfill(11)
res = ""
for i in range(bytes_req - 1):
res = str_unicode[-6:] + res
res = "10" + res
str_unicode = str_unicode[:-6]
res = start + str_unicode + res
res = hex(int(res, 2)).upper()
return res[2:]
def convert_to_utf16(unicode):
if int(unicode, 16) <= 0xFFFF:
unicode = unicode[-4:]
return unicode.zfill(4)
subtracted_val = int(unicode, 16) - 0x10000
binary_val = bin(subtracted_val)[2:].zfill(20)
left, right = binary_val[0:10], binary_val[10:20]
final_left = hex(0xD800 + int(left, 2))[2:].upper()
final_right = hex(0xDC00 + int(right, 2))[2:].upper()
return final_left + final_right
def convert_to_utf32(unicode):
unicode = unicode[-8:]
return unicode.zfill(8)
def check_and_convert_to_utf8(unicode):
unicode = unicode.replace(' ', '').upper()
try:
if is_valid_unicode(unicode):
unicode = unicode.upper()
utf8 = convert_to_utf8(unicode)
return f"UTF-8: {' '.join(utf8[i:i+2] for i in range(0, len(utf8), 2))}"
else:
return ("Invalid input.")
except ValueError:
return ("Error: Invalid Unicode input. Please provide valid Unicode text.")
def check_and_convert_to_utf16(unicode):
unicode = unicode.replace(' ', '').upper()
try:
if is_valid_unicode(unicode):
unicode = unicode.upper()
utf16 = convert_to_utf16(unicode)
return f"UTF-16: {' '.join(utf16[i:i+2] for i in range(0, len(utf16), 2))}"
else:
return ("Invalid input.")
except ValueError:
return ("Error: Invalid Unicode input. Please provide valid Unicode text.")
def check_and_convert_to_utf32(unicode):
unicode = unicode.replace(' ', '').upper()
try:
if is_valid_unicode(unicode):
unicode = unicode.upper()
utf32 = convert_to_utf32(unicode)
return f"UTF-32: {' '.join(utf32[i:i+2] for i in range(0, len(utf32), 2))}"
else:
return ("Invalid input.")
except ValueError:
return ("Error: Invalid Unicode input. Please provide valid Unicode text.")
def main():
print("---------Unicode Converter---------")
input_unicode = input("Input (Unicode): U+")
try:
if is_valid_unicode(input_unicode):
input_unicode = input_unicode.upper()
utf8 = convert_to_utf8(input_unicode)
utf16 = convert_to_utf16(input_unicode)
utf32 = convert_to_utf32(input_unicode)
print("Outputs:")
print("UTF-8:", ' '.join(utf8[i:i+2] for i in range(0, len(utf8), 2)))
print("UTF-16:", ' '.join(utf16[i:i+2] for i in range(0, len(utf16), 2)))
print("UTF-32:", ' '.join(utf32[i:i+2] for i in range(0, len(utf32), 2)))
else:
print("Invalid input.")
except ValueError:
print("Error: Invalid Unicode input. Please provide valid Unicode text.")
if __name__ == "__main__":
main()