Python 没有编码的字符串参数
Python string argument without an encoding
我正在尝试 运行 这段代码,但它一直报错说 "String argument without an encoding"
ota_packet = ota_packet.encode('utf-8') + bytearray(content[current_pos:(final_pos)]) + '[=11=]'.encode('utf-8')
有什么帮助吗?
您正在将字符串对象传递给 bytearray()
:
bytearray(content[current_pos:(final_pos)])
您需要提供编码参数(第二个参数),以便将其编码为字节。
例如,您可以将其编码为 UTF-8:
bytearray(content[current_pos:(final_pos)], 'utf8')
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 using str.encode()
.
byteObject = b'\x18,\xa3\xf0A\x93*<bAd\x15K.A\xba'
print(byteObject)
print('-----------asbytearray----------')
print('-------As a string------------------')
o = base64.b64encode(bytes(str(byteObject), 'utf-8'))
print(o.decode("utf-8"))`enter code here`
print('--------Nonce as a string------------------')
我正在尝试 运行 这段代码,但它一直报错说 "String argument without an encoding"
ota_packet = ota_packet.encode('utf-8') + bytearray(content[current_pos:(final_pos)]) + '[=11=]'.encode('utf-8')
有什么帮助吗?
您正在将字符串对象传递给 bytearray()
:
bytearray(content[current_pos:(final_pos)])
您需要提供编码参数(第二个参数),以便将其编码为字节。
例如,您可以将其编码为 UTF-8:
bytearray(content[current_pos:(final_pos)], 'utf8')
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()
.
byteObject = b'\x18,\xa3\xf0A\x93*<bAd\x15K.A\xba'
print(byteObject)
print('-----------asbytearray----------')
print('-------As a string------------------')
o = base64.b64encode(bytes(str(byteObject), 'utf-8'))
print(o.decode("utf-8"))`enter code here`
print('--------Nonce as a string------------------')