I am working with UDP in python 3.8.0, but I am having a problem with my message, once the socket doesn't allow a string, I have to convert the string to binary, so I used message.encode()
, but it comes with an additional b' and ' at the end of the message, how can I remove them?
My code:
import socket
import sys
import config
MY_IP = config.myIP
OTHER_IP = config.otherIp
PORT_NUMBER = config.port_number
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = (MY_IP, PORT_NUMBER)
message = input("Write a message: ").encode( )
print("Message: ", message)
The output of the result you can see below:
Write a message: a
Message: b'a'
Is there another way to change string to binary?
A new python learner thanks you.
b'...'
is just how python represents the bytes type. For example:
my_string = "This is my string."
type(my_string) # str type
encoded_string = my_string.encode()
type(encoded_string) # bytes type, when printing represented by leading b
You can convert a bytes type back to a str using the built-in .decode()
method:
decoded_string = encoded_string.decode()
type(decoded_string) # str type
Extra:
You also are able to specify the encoding charset used by .encode()
and .decode()
:
hello_encoded = "Hello".encode("ascii") # bytes type
hello_decoded = hello_encoded.decode("ascii") # str type
When encoding with utf-8 you can write all sorts of fancy characters (those are not possible to encode with ascii, would throw an error):
fancy_chars_encoded = "© ½ £ 4²".encode("utf-8")
fancy_chars_decoded = fancy_chars_encoded.decode("utf-8")
Also, utf-8
is the default charset used when no charset is passed to .encode()
or .decode()
.