utf-16 encoding #13481
Unanswered
prabhu-yu
asked this question in
Libraries & Drivers
utf-16 encoding
#13481
Replies: 2 comments
-
The only encoding supported by MicroPython (as far as I know), for size reasons, is UTF-8. It simply ignores the encoding argument to any functions that take one and always uses UTF-8. You will have to implement the conversion yourself (or find a library that has already done it). |
Beta Was this translation helpful? Give feedback.
0 replies
-
I implemented a very crude UTF-16BE encoder in my micropython-SYN6988 speech synthesizer project. It might do what you need: def fauxutf16(s, badchar=" "):
# encodes string s as UTF-16BE, two bytes per char, no BOM
# eg: "hello" => b'\x00h\x00e\x00l\x00l\x00o'
# char codes > 65535 encoded as badchar
# MicroPython can only encode to UTF-8 bytes, so we need this
buf = bytearray(len(s) * 2) # preallocate empty buffer
for i, c in enumerate(s):
n = ord(c)
if n > 65535:
n = ord(badchar)
buf[2 * i] = n // 256
buf[2 * i + 1] = n % 256
return bytes(buf)
s = '8'
t = fauxutf16(s)
for i in t:
print(i) Output from MicroPython and Python is:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I want to encode a string into utf-16 bytes.
In Python3, I know, but in Micropython, I do not.
The following example code works in Python3(Ubuntu Linux) but not in MicroPython (ESP32/RPI Pico).
Some please let me know how to achieve this.
Beta Was this translation helpful? Give feedback.
All reactions