Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Odd-length string

1,857 views
Skip to first unread message

mark...@gmail.com

unread,
Oct 6, 2013, 9:10:16 AM10/6/13
to
print("Key-" + str(võti) + ": " + str(unhexlify("".join(tulemus))))

IM getting this error on this line. This is the print line of a decryption program. I wanti it to convert the tulemus which is in HEX to ASCII so i could read it. I could use online translators for the line, but since i have 255 lines of the HEX codes, it would be higly unefficient.

How to i fix the Oddlenght string?

Roy Smith

unread,
Oct 6, 2013, 9:46:03 AM10/6/13
to
In article <0f1b6cd6-3025-4bdc...@googlegroups.com>,
mark...@gmail.com wrote:

> print("Key-" + str(v�ti) + ": " + str(unhexlify("".join(tulemus))))
Some basic advice on asking for help:

1) Don't tell us what the error was, show us. Run your program, then
cut-and-paste the entire stack trace into your post.

2) Tell us what version of Python you're using. I'm assume one of the
Python 3 versions, since you put ()'s after print, but don't make people
guess.

3) Reduce the problem down to the smallest possible amount of code. Do
you get the error when you do:

print(str(unhexlify("".join(tulemus))))

what about

print(unhexlify("".join(tulemus)))

or

print("".join(tulemus))

or

print(str(v�ti))

every little thing you can hack away and still generate the error gets
you (and us!) one step closer to understanding what's happening.

Peter Otten

unread,
Oct 6, 2013, 10:31:11 AM10/6/13
to pytho...@python.org
My crystal ball says: strip off the newline byte:

>>> unhexlify(tulemus)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
binascii.Error: Odd-length string
>>> unhexlify(tulemus.rstrip(b"\n"))
b'whatever'


mark...@gmail.com

unread,
Oct 6, 2013, 10:37:13 AM10/6/13
to
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.

Or does anyone have a better idea how to translate HEX into ASCII and sort out the lines that make sense

Mark Lawrence

unread,
Oct 6, 2013, 10:56:05 AM10/6/13
to pytho...@python.org
Will you please be kind enough to quote your replies in context.

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence

Terry Reedy

unread,
Oct 6, 2013, 3:50:38 PM10/6/13
to pytho...@python.org
On 10/6/2013 10:37 AM, mark...@gmail.com wrote:

> 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 ''.join(iterable_of_strings) executes, the result is a string and str
would not be needed. If you try
"".join(tulemus2)
by itself, you will see the same error.

> 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']

Since tulemus2 is an iterable_of_bytes, you must join with bytes b'' and
call str on the result. So just add the b prefix.

What Roy tried to say to you earlier is that when you get an error in a
expression, and you do not understand *exactly* where in the expression
the error is, pull the expression apart and test the pieces
individually. This sometimes includes splitting

print(expression) # into

s=expression
print(s)

--
Terry Jan Reedy


Piet van Oostrum

unread,
Oct 6, 2013, 8:59:24 PM10/6/13
to
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 <pi...@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
0 new messages