Monday, 15 June 2015

How to convert python str to bytearray -



How to convert python str to bytearray -

i'm using python 2.7 , need convert homecoming value of strcut.pack() function (which str according docs) bytearray object. bytearray() builtin function accepts string value, docs must pass encoding parameter used encode string bytes. don't know encoding should since struct.pack() homecoming value presumably should in byte values, it's not actual text string, there no encoding.

so how can turn bytes object stored in python2 str bytearray object?

update: documentation has been corrected; don't need utilize encoding parameter when passing in string, section correctly tells needed unicode objects.

my old reply follows below.

the documentation wrong; copied wholesale python 3 encoding parameter indeed required (unicode) string has encoded bytes first.

on python 2, pass in string, no encoding specified:

>>> bytearray('the quick brownish fox jumps on lazy dog') bytearray(b'the quick brownish fox jumps on lazy dog')

and mentally replace string in documentation unicode string.

i've filed new issue in python tracker have corrected.

python python-2.7 bytearray

No comments:

Post a Comment