我正在尝试运行这段代码,它不断给出错误提示“没有编码的字符串参数”
ota_packet = ota_packet.encode('utf-8') + bytearray(content[current_pos:(final_pos)]) + '\0'.encode('utf-8')
有什么帮助吗?
最佳答案
您正在将一个字符串对象传递给 bytearray()
:
bytearray(content[current_pos:(final_pos)])
您需要提供一个编码参数(第二个参数),以便将其编码为字节。
例如,您可以将其编码为 UTF-8:
bytearray(content[current_pos:(final_pos)], 'utf8')
来自 bytearray()
documentation :
The optional source parameter can be used to initialize the array in a few different ways:
- If it is a string, you must also give the encoding (and optionally, errors) parameters;
bytearray()
then converts the string to bytes usingstr.encode()
.
关于没有编码的 Python 字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31161243/