Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #56252 > unrolled thread
| Started by | markotaht@gmail.com |
|---|---|
| First post | 2013-10-06 06:10 -0700 |
| Last post | 2013-10-06 20:59 -0400 |
| Articles | 7 — 6 participants |
Back to article view | Back to comp.lang.python
Odd-length string markotaht@gmail.com - 2013-10-06 06:10 -0700
Re: Odd-length string Roy Smith <roy@panix.com> - 2013-10-06 09:46 -0400
Re: Odd-length string Peter Otten <__peter__@web.de> - 2013-10-06 16:31 +0200
Re: Odd-length string markotaht@gmail.com - 2013-10-06 07:37 -0700
Re: Odd-length string Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-06 15:56 +0100
Re: Odd-length string Terry Reedy <tjreedy@udel.edu> - 2013-10-06 15:50 -0400
Re: Odd-length string Piet van Oostrum <piet@vanoostrum.org> - 2013-10-06 20:59 -0400
| From | markotaht@gmail.com |
|---|---|
| Date | 2013-10-06 06:10 -0700 |
| Subject | Odd-length string |
| Message-ID | <0f1b6cd6-3025-4bdc-8f80-73a51a7ed241@googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-10-06 09:46 -0400 |
| Message-ID | <roy-7CB56C.09460306102013@news.panix.com> |
| In reply to | #56252 |
In article <0f1b6cd6-3025-4bdc-8f80-73a51a7ed241@googlegroups.com>,
markotaht@gmail.com wrote:
> 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?
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.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-10-06 16:31 +0200 |
| Message-ID | <mailman.783.1381069884.18130.python-list@python.org> |
| In reply to | #56252 |
markotaht@gmail.com wrote:
> 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?
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'
[toc] | [prev] | [next] | [standalone]
| From | markotaht@gmail.com |
|---|---|
| Date | 2013-10-06 07:37 -0700 |
| Message-ID | <dc7e43a4-2f40-4275-8754-7e947f886fd9@googlegroups.com> |
| In reply to | #56252 |
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
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-10-06 15:56 +0100 |
| Message-ID | <mailman.784.1381071381.18130.python-list@python.org> |
| In reply to | #56260 |
On 06/10/2013 15:37, markotaht@gmail.com wrote:
> 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
>
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
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-10-06 15:50 -0400 |
| Message-ID | <mailman.794.1381089050.18130.python-list@python.org> |
| In reply to | #56260 |
On 10/6/2013 10:37 AM, markotaht@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
[toc] | [prev] | [next] | [standalone]
| From | Piet van Oostrum <piet@vanoostrum.org> |
|---|---|
| Date | 2013-10-06 20:59 -0400 |
| Message-ID | <m2y565zyj7.fsf@cochabamba.vanoostrum.org> |
| In reply to | #56260 |
markotaht@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@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web