Skip to content Skip to sidebar Skip to footer

Iron Python: How To Append String To Bytearray

I have a bytearray to which I have to add a number as a four character string. i.g. 14 should be added as '0014'. I tried this: id = 14 arr.append(bytearray(format(id, '04x')))

Solution 1:

Really you should explicitly specify the encoding when converting to bytes from a string. This answer also works in python 3:

arr.extend(format(id, "04x").encode('ascii'))

Solution 2:

arr.extend(bytes(format(id,"04x")))

Post a Comment for "Iron Python: How To Append String To Bytearray"