【发布时间】:2013-07-11 18:12:41
【问题描述】:
我正在使用 pyserial,需要发送一些小于 255 的值。如果我发送 int 本身,则会发送 int 的 ascii 值。所以现在我将 int 转换为 unicode 值并通过串口发送它。
unichr(numlessthan255);
However it throws this error:
'ascii' codec can't encode character u'\x9a' in position 24: ordinal not in range(128)
将 int 转换为 unicode 的最佳方法是什么?
【问题讨论】:
-
Python2 还是 Python3? (猜测 Python2,但差别很大)你确定
unichr是呼叫崩溃了吗?您如何实际发送 unichr 返回的数据? -
unichr()在 Python 3 中不存在,所以这是 Python 2。unichr()在 Python 3 中被命名为chr()(转换为 Unicode 字符)。
标签: python character-encoding ascii pyserial