efinf:blcks2017:bitsundbytes:utf8

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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()
 </code> </code>
 +
 +<hidden Lösungsvorschlag>
 +<code python>
 +def to_utf8(n):
 +    if (n<128):
 +        return chr(n)
 +    if (n<0x7ff):
 +        b1 = 0b11000000 | (n >> 6)
 +        b2 = 0b10000000 | (n & 0x3f)
 +        return chr(b1)+chr(b2)
 +    if (n<0xffff):
 +        b1 = 0b11100000 | (n >> 12)
 +        b2 = 0b10000000 | ((n>>6) & 0x3f)
 +        b3 = 0b10000000 | (n & 0x3f)
 +        return chr(b1)+chr(b2)+chr(b3)
 +    else:
 +        b1 = 0b11110000 | (n >> 18)
 +        b2 = 0b10000000 | ((n>>12) & 0x3f)
 +        b3 = 0b10000000 | ((n>>6) & 0x3f)
 +        b4 = 0b10000000 | (n & 0x3f)
 +        return chr(b1)+chr(b2)+chr(b3)+chr(b4)
 +</code>
 +</hidden>
  • efinf/blcks2017/bitsundbytes/utf8.1511254572.txt.gz
  • Last modified: 2017/11/21 09:56
  • by Ivo Blöchliger