Differences
This shows you the differences between two versions of the page.
| efinf:blcks2017:bitsundbytes:utf8 [2017/11/21 08:56] – created Ivo Blöchliger | efinf:blcks2017:bitsundbytes:utf8 [2017/11/21 08:58] (current) – Ivo Blöchliger | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | Vervollständigen Sie folgenden Code: | ||
| + | <code python utf8html.py> | ||
| + | def to_utf8(n): | ||
| + | # hier die UTF8-Byte-Sequenz als String generieren | ||
| + | | ||
| + | def header(): | ||
| + | return '< | ||
| + | def footer(): | ||
| + | return '</ | ||
| + | |||
| + | def shownice(code, | ||
| + | utf8 = to_utf8(code); | ||
| + | res = utf8+" "+ title+" | ||
| + | for i in range(len(utf8)): | ||
| + | res += str(ord(utf8[i])) + " | ||
| + | res += " < | ||
| + | for i in range(len(utf8)): | ||
| + | res += hex(ord(utf8[i])) + " | ||
| + | res += " < | ||
| + | for i in range(len(utf8)): | ||
| + | res += bin(ord(utf8[i])) + " | ||
| + | return res | ||
| + | | ||
| + | | ||
| + | out = open(" | ||
| + | out.write(header()) | ||
| + | out.write(shownice(0x260e," | ||
| + | out.write(footer()) | ||
| + | out.close() | ||
| + | </ | ||
| + | |||
| + | <hidden Lösungsvorschlag> | ||
| + | <code python> | ||
| + | def to_utf8(n): | ||
| + | if (n<128): | ||
| + | return chr(n) | ||
| + | if (n< | ||
| + | b1 = 0b11000000 | (n >> 6) | ||
| + | b2 = 0b10000000 | (n & 0x3f) | ||
| + | return chr(b1)+chr(b2) | ||
| + | if (n< | ||
| + | b1 = 0b11100000 | (n >> 12) | ||
| + | b2 = 0b10000000 | ((n>> | ||
| + | b3 = 0b10000000 | (n & 0x3f) | ||
| + | return chr(b1)+chr(b2)+chr(b3) | ||
| + | else: | ||
| + | b1 = 0b11110000 | (n >> 18) | ||
| + | b2 = 0b10000000 | ((n>> | ||
| + | b3 = 0b10000000 | ((n>> | ||
| + | b4 = 0b10000000 | (n & 0x3f) | ||
| + | return chr(b1)+chr(b2)+chr(b3)+chr(b4) | ||
| + | </ | ||
| + | </ | ||