문제

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')))

but it results in: TypeError: unicode argument without an encoding

도움이 되었습니까?

해결책

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'))

다른 팁

arr.extend(bytes(format(id,"04x")))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top