47

Am trying to a run this piece of code, and it keeps giving an error saying "String argument without an encoding"

ota_packet = ota_packet.encode('utf-8') + bytearray(content[current_pos:(final_pos)]) + '\0'.encode('utf-8')

Any help?

the
  • 21,007
  • 11
  • 68
  • 101
lonely
  • 681
  • 1
  • 7
  • 10
  • Do you want to get: `(ota_packet + content[current_pos:final_pos] + '\0').encode('utf-8')`? – jfs Jul 01 '15 at 15:17

2 Answers2

71

You are passing in a string object to a bytearray():

bytearray(content[current_pos:(final_pos)])

You'll need to supply an encoding argument (second argument) so that it can be encoded to bytes.

For example, you could encode it to UTF-8:

bytearray(content[current_pos:(final_pos)], 'utf8')

From the 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 using str.encode().
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0
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------------------')
nick
  • 346
  • 2
  • 4