Differences
This shows you the differences between two versions of the page.
efinf:blcks2017:bitsundbytes:utf8 [2017/11/21 09:56] Ivo Blöchliger created |
efinf:blcks2017:bitsundbytes:utf8 [2017/11/21 09:58] (current) Ivo Blöchliger |
||
---|---|---|---|
Line 30: | Line 30: | ||
out.close() | 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) | ||
+ | </ | ||
+ | </ |