Created
December 12, 2016 15:10
-
-
Save v2e4lisp/9270449b29ebd788b0cb6921e7dcf6a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
def utf8cut(ba, l): | |
if len(ba) <= l: | |
return ba | |
pos = l | |
while (pos > 0): | |
b = ba[pos] | |
if (b & 0x80 == 0) or (b & 0xc0 == 0xc0): | |
break | |
else: | |
pos -= 1 | |
return ba[0:pos] | |
x = bytearray("1世2界1") | |
for i in range(len(x)): | |
print i, "= [", str(utf8cut(x, i)), "]" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment