forked from xxEnesx/infotrainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUTF8.py
76 lines (65 loc) · 2.24 KB
/
UTF8.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
def UTF8_ui():
print('UTF-8 Masken:')
print(' 0000 - 007F: 0xxxxxxx')
print(' 0080 - 07FF: 110xxxxx 10xxxxxx')
print(' 0800 - FFFF: 1110xxxx 10xxxxxx 10xxxxxx')
print('0001 0000 - 0010 FFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx')
dezimal = input('Codepoint Dezimal: ')
if int(dezimal) > int('10FFFF', 16):
print('Dezimaler Codepoint zu groß!')
return
input('Codepoint Hexadezimal: ')
print('Codepoint Hexadezimal: ' + str(hex(int(dezimal)))[2:])
input('Codepoint Binär: ')
binaer_str = getBinaer_str(dezimal)
print('Codepoint Binär: ' + binaer_str)
input('Maske:')
if int(dezimal) < int('007F', 16):
print('Maske: 0xxxxxxx')
mask = '0xxxxxxx'
elif int(dezimal) < int('07FF', 16):
print('Maske: 110xxxxx 10xxxxxx')
mask = '110xxxxx 10xxxxxx'
elif int(dezimal) < int('FFFF', 16):
print('Maske: 1110xxxx 10xxxxxx 10xxxxxx')
mask = '1110xxxx 10xxxxxx 10xxxxxx'
elif int(dezimal) < int('10FFFF', 16):
print('Maske: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx')
mask = '11110xxx 10xxxxxx 10xxxxxx 10xxxxxx'
input('Eingesetzt in Maske: ')
mask = getMask(dezimal, mask)
print('Eingesetzt in Maske: ' + mask)
input('In 4-er Blöcke eingeteilt: ')
mask = maskEinteilen(mask)
print('In 4-er Blöcke eingeteilt: ' + mask)
input('UTF-8 Codierung: ')
mask = mask.replace(' ', '')
print('UTF-8 Codierung: ' + str(hex(int(mask, 2)))[2:])
def maskEinteilen(mask):
i = 4
while i < len(mask):
mask = mask[:i] + ' ' + mask[i:]
i += 10
return mask
def getMask(dezimal, mask):
binaer_str = str(bin(int(dezimal)))[2:]
u = len(binaer_str) - 1
mask = list(mask)
for i in range(len(mask) - 1, -1, -1):
if mask[i] == 'x':
if u < 0:
mask[i] = '0'
else:
mask[i] = binaer_str[u]
u -= 1
mask = ''.join(mask)
return mask
def getBinaer_str(dezimal):
binaer_str = str(bin(int(dezimal)))[2:]
i = len(binaer_str)-4
while i > 0:
binaer_str = binaer_str[:i] + ' ' + binaer_str[i:]
i -= 4
return binaer_str
if __name__ == '__main__':
UTF8_ui()