Odd-length string

Piet van Oostrum piet at vanoostrum.org
Sun Oct 6 20:59:24 EDT 2013


markotaht at gmail.com writes:

> I fixed this problem but encountered new problem. Problem was that some parts that came throug my decryption were 00 or 0 the first symbol so the program didnt show them. 
>
> NEw problem is : Traceback (most recent call last):
>   File "C:\Users\Marko\Desktop\hacker.org\XOR cypher.py", line 35, in <module>
>     print("Key-" + str(võti) + ": " + str("".join(tulemus2)))
> TypeError: sequence item 0: expected str instance, bytes found
>
> If i  take away the join command i get this: Key-00000000: [b'u', b'o', b'\x00', b'\x1d', b' ', b'|', b'N', b'\x0f', b'9', b'j', b'K', b'J', b'&', b'#', b'A', b'K', b'5', b'k', b'_', b'\x1e', b',', b'j', b'\x0c', b'\x08', b'i', b'(', b'\x06', b'\\', b'r', b'3', b'\x1f', b'V', b's', b'9', b'\x1d']
>
> the Key-00000000 is the key im using to decrypt the code. everything else is generated byt the decrytion process and the unhexlify command. So my guess is, the join command cant handle the b"u" type of format. how can i get rid of the b.

Use b''.join(tulemus2), and then to convert it to a string you have to specify the encoding, which should be 'ascii', as you say it is ASCII.

str(b''.join(tulemus2), 'ascii')

or

b''.join(tulemus2).decode('ascii')

But note: If your tulemus2 contains bytes > 127 the this will fail as it is then not ASCII.
>
> Or does anyone have a better idea how to translate HEX into ASCII and
> sort out the lines that make sense

You can use base64.b16decode, but it needs a byte string as input. For the rest is is the same as unhexlify. And both will give you a byte string, because there is no way to translate a hex string to a character string without specifying an encoding (if there is hex > 7F it is no longer ASCII).

-- 
Piet van Oostrum <piet at vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]



More information about the Python-list mailing list