i saw that if i want to send in bytes something, i have to write s.send(b'your text'). That is good so far. But what happens if 'your text' is stored in a variable (like a = 'your text') and i want to send the variable a in bytes?
I assumes and tested:
s.send(b 'a') #does not work,it will send only 'a' as byte
s.send(b(a)) #does not work, error 'b is not defined'
-
Does this answer your question? Add "b" prefix to python variable?Nikolaos Chatzis– Nikolaos Chatzis2021年04月21日 20:24:29 +00:00Commented Apr 21, 2021 at 20:24
1 Answer 1
If you are using python3, you probably want the bytes function (which will in turn direct you to the docs of the bytesarray function for the detail).
You need to know the encoding of your your text in order to convert it to bytes. From the docs:
If [the source] is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().
So you will want something like s.send(bytes(a, 'utf-8')).