let say i got string(that is supposed to be a bytes):
"b'YzJGNVltYzJGNVltOTBlbk5oZVdKdmRIcHpZWGxpYjNSNmMyRjVZbTkwZW5OaGVXSnZkSHB6WVhsaWIzUjZjMkY1WW05MGVnOTBlblJsYzNRPXNheWJvdHo='"
and i want to decode it but it say it got to be in bytes so i got to turn that to this somehow
b'YzJGNVltYzJGNVltOTBlbk5oZVdKdmRIcHpZWGxpYjNSNmMyRjVZbTkwZW5OaGVXSnZkSHB6WVhsaWIzUjZjMkY1WW05MGVnOTBlblJsYzNRPXNheWJvdHo='
i am working on a chat client and when it sent the data(bytes), it turn into a string somehow when passing though the server and when it reach the other side, it can't decode it
ps: it is ENCRYPT, decoding that will give random number and letters, the client will decrypt it
oh, and, i don't really know if the '=' sign is supposed to be there, when i got the data in a test between me and my friend, i got it like that string(the one on the top) with the error saying that it got to be in byte.
part of the code in the client(just one line to show everyone, the rest is a secret):
base64.b64decode(that_string).decode('ascii')
it mainly use ascii so i think this is right, right?
more info:
base64.b64encode(message.encode('ascii'))
the message here is for getting the string sent from the other side
Room.message(str(secretEncrypt(par, codes())))
i don't know how i miss this, it str it before it send =.= well, it still need to turn it to bytes, how so i do that?
3 Answers 3
The following greatly works
>>> import base64
>>> s = "Hello StackOwerflow!"
>>> b = s.encode()
>>> c = base64.b64encode(b)
>>> c
b'SGVsbG8gU3RhY2tPd2VyZmxvdyE='
>>> b1 = base64.b64decode(c)
>>> b1
b'Hello StackOwerflow!'
>>> b1.decode()
'Hello StackOwerflow!'
Show how you encode string
6 Comments
The problem is here:
Room.message(str(secretEncrypt(par, codes())))
secretEncrypt(...) returns bytes, but then you do str(b"thereturnvalue") which does not do what you want:
>>> my_bytes = b"abc"
>>> type(my_bytes)
<class 'bytes'>
>>> str(my_bytes)
"b'abc'"
>>> type(str(my_bytes))
<class 'str'>
It is putting the string-representation (repr) of your bytes into a string, which is why your string starts with b"
How best to fix this depends on the surrounding code.. Either:
- if the
secretEncryptfunction returns bytes, makeRoom.messageaccept bytes also (and remove thestr(...)cast). - Make
secretEncryptreturn a string, removing the need for thestr(...)cast Least elegant solution: decode the bytes returned from
secretEncrypt, turning it into a string. Something like this:encrypted = secretEncrypt(par, codes()) Room.message(encrypted.decode("whatever-encoder-was-used"))Where
whatever-encoding-was-usedis the text encoding (likelyutf-8orascii, but it is impossible to tell without seeing the rest of your code)
3 Comments
secretEncrypt return a string, so you can remove the str() call. I've added another solution (decoding the the bytes returned by secretEncrypt, which can then be passed to Room.message)and i was right, no need to over think it
the string:
"b'YzJGNVltYzJGNVltOTBlbk5oZVdKdmRIcHpZWGxpYjNSNmMyRjVZbTkwZW5OaGVXSnZkSHB6WVhsaWIzUjZjMkY1WW05MGVnOTBlblJsYzNRPXNheWJvdHo='"
just need to be split, eg:
string = "b'YzJGNVltYzJGNVltOTBlbk5oZVdKdmRIcHpZWGxpYjNSNmMyRjVZbTkwZW5OaGVXSnZkSHB6WVhsaWIzUjZjMkY1WW05MGVnOTBlblJsYzNRPXNheWJvdHo='"
string = string.split("'")[1]
and that would get me a string without the b':
"YzJGNVltYzJGNVltOTBlbk5oZVdKdmRIcHpZWGxpYjNSNmMyRjVZbTkwZW5OaGVXSnZkSHB6WVhsaWIzUjZjMkY1WW05MGVnOTBlblJsYzNRPXNheWJvdHo="
then i just need to bytes it:
string = bytes(string, "ascii")
which would get me the b' outside the string:
b"YzJGNVltYzJGNVltOTBlbk5oZVdKdmRIcHpZWGxpYjNSNmMyRjVZbTkwZW5OaGVXSnZkSHB6WVhsaWIzUjZjMkY1WW05MGVnOTBlblJsYzNRPXNheWJvdHo="
ps: to everyone out there who say things about cracking my code, it got randoms mumbo jumbo things with the id dump into it, if you manage to crack it, it not much use and anyway, it just a chat client for a mini chatroom games for kids
repr(data)instead of justdata. Show the code doing the sending and we can fix the right problem.base64.b64encode(...)returns bytes, so how is it sent to the client?bytesthrough astrwithout decoding/encoding (in other words, send each byte as the Unicode codepoint with the same number). This isn't impossible—but it's almost always a sign that you're doing something wrong. There's almost always a better answer. For example: If your API is byte-based, usebytesinstead ofstrin the API; if yourbytesare ASCII (which is always true for base64), justb.decode('ascii')to get thestr; etc.