我试图运行这段代码,但它不断给出一个错误,说"字符串参数没有编码"
ota_packet = ota_packet.encode('utf-8') + bytearray(content[current_pos:(final_pos)]) + '\0'.encode('utf-8')
有什么帮助吗?
我试图运行这段代码,但它不断给出一个错误,说"字符串参数没有编码"
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')
可选的source参数可用于以几种不同的方式初始化数组:
- 如果是字符串,还必须给出encoding个(可选errors个)参数;
bytearray()
然后使用str.encode()
将字符串转换为字节.