When using Python to do network programming will inevitably encounter convert a string to the byte stream,Here we record the following commonly used methods:
First byte array to a string,That is str:
1 2 3 4 5 |
b = b'some byte array' str(b, encoding = "utf-8") #or bytes.decode(b) |
Then the string to a byte array:
1 2 3 4 5 |
s = 'some string' bytes(s, encoding = "utf8") #or str.encode(s) |
Either the above two methods are possible。
Original article written by LogStudio:R0uter's Blog » Python strings and arrays of bytes
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1686.html
Dry ah