Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct stdout_write_bytes for UTF-8 chars. #106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions ampy/pyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,35 @@
# Python2 doesn't have buffer attr
stdout = sys.stdout

utf8Char = b""
counts = 3
def stdout_write_bytes(b):
b = b.replace(b"\x04", b"")
stdout.write(b)
stdout.flush()
global utf8Char, counts

for i in b:
curr = b'' + b
if curr==b'\x04': # end of output
return;
elif curr <= b'\x7f': # 1-byte UTF-8 char
utf8Char = curr
counts = 0
elif b'\xC0' <= curr <= b'\xDF': # 2-bytes UTF-8 char
utf8Char = curr
counts = 1
elif b'\xE0' <= curr <= b'\xEF': # 3-bytes UTF-8 char
utf8Char = curr
counts = 2
elif b'\xF0' <= curr <= b'\xF7': # 4-bytes UTF-8 char
utf8Char = curr
counts = 3
elif b'\x80' <= curr <= b'\xBF': # 1 byte in a UTF-8 char
utf8Char = utf8Char + curr
counts = counts - 1

if counts == 0: # 1 UTF-8 char readed
print(utf8Char.decode('UTF-8'), end="")
counts = 3 # reset count
utf8Char=b''

class PyboardError(BaseException):
pass
Expand Down