1

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.

Codewraith
  • 103
  • 1
  • 8

2 Answers2

3

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().

sp4c38
  • 455
  • 3
  • 14
3

You did it correct. And this b only means that this string is byte. This is not a part of your variable. For example:

>>> a = "abc".encode()
>>> print(a)
b'abc'
>>> print(len(a))
3 # not 6
>>> print(a[0])
'a' # not b
Ekrem Dinçel
  • 1,053
  • 6
  • 17
  • I need to send the message to another computer using the UDP connection (`sent = sock.sendto(message, server_address)`), won't this b' matter at all? Is it just some kind of sign? – Codewraith Nov 16 '19 at 20:37
  • 3
    @Codewraith no, **there is no `b`** in those bytes. – juanpa.arrivillaga Nov 16 '19 at 20:54
  • No, this ```b``` dont matter. It is only a way to python show you that type of your variable is bytes. If my answer helped you, please mark it as answer. – Ekrem Dinçel Nov 17 '19 at 05:45