[Tutor] Fwd: string to binary and back... Python 3

Jordan wolfrage8765 at gmail.com
Thu Jul 19 20:40:09 CEST 2012



On 07/19/2012 10:29 AM, Walter Prins wrote:
> Hi,
>
> Just to show you your original message contained no indentation
> whatsoever.  You might want to check your mail client settings and do
> some experiments to make sure that indentation spaces are let through
> unmolested and not stripped anywhere, otherwise the current little
> brouhaha about formatting will result.  You have to admit, it's not
> easy to read the code below with zero indentation present... :)
Thank you for pointing that out, I did not realize it as I had copied
and pasted it from the python file I was working on. I guess Thunderbird
edited the email on me, even though I had put it into plain text mode.
Next time perhaps I will just attach the file if that is acceptable
rather than getting attacked for what my mail editor did.
>
> Regards
>
> Walter
>
>
> ---------- Forwarded message ----------
> From: Jordan <wolfrage8765 at gmail.com>
> Date: 18 July 2012 22:07
> Subject: [Tutor] string to binary and back... Python 3
> To: tutor at python.org
>
>
> OK so I have been trying for a couple days now and I am throwing in the
> towel, Python 3 wins this one.
> I want to convert a string to binary and back again like in this
> question: Stack Overflow: Convert Binary to ASCII and vice versa
> (Python)
> <http://stackoverflow.com/questions/7396849/convert-binary-to-ascii-and-vice-versa-python>
> But in Python 3 I consistently get  some sort of error relating to the
> fact that nothing but bytes and bytearrays support the buffer interface
> or I get an overflow error because something is too large to be
> converted to bytes.
> Please help me and then explian what I am not getting that is new in
> Python 3. I would like to point out I realize that binary, hex, and
> encodings are all a very complex subject and so I do not expect to
> master it but I do hope that I can gain a deeper insight. Thank you all.
>
> test_script.py:
> import binascii
>
> test_int = 109
>
> test_int = int(str(test_int) + '45670')
> data = 'Testing XOR Again!'
>
> while sys.getsizeof(data) > test_int.bit_length():
>
> test_int = int(str(test_int) + str(int.from_bytes(os.urandom(1), 'big')))
>
> print('Bit Length: ' + str(test_int.bit_length()))
>
> key = test_int # Yes I know this is an unnecessary step...
>
> data = bin(int(binascii.hexlify(bytes(data, 'UTF-8')), 16))
>
> print(data)
>
> data = int(data, 2)
>
> print(data)
>
> data = binascii.unhexlify('%x' % data)
>
>
> wolfrage at lm12-laptop02 ~/Projects $ python3 test_script.py
> Bit Length: 134
> 0b10101000110010101110011011101000110100101101110011001110010000001011000010011110101001000100000010000010110011101100001011010010110111000100001
> 7351954002991226380810260999848996570230305
> Traceback (most recent call last):
> File "test_script.py", line 24, in <module>
> data = binascii.unhexlify('%x' % data)
> TypeError: 'str' does not support the buffer interface
>
>
>
> test_script2.py:
> import binascii
> test_int = 109
> test_int = int(str(test_int) + '45670')
> data = 'Testing XOR Again!'
> while sys.getsizeof(data) > test_int.bit_length():
> test_int = int(str(test_int) + str(int.from_bytes(os.urandom(1), 'big')))
> print('Bit Length: ' + str(test_int.bit_length()))
> key = test_int # Yes I know this is an unnecessary step...
> data = bin(int(binascii.hexlify(bytes(data, 'UTF-8')), 16))
> print(data)
> data = int(data, 2)
> print(data)
> data = binascii.unhexlify(bytes(data, 'utf8'))
>
>
>
> wolfrage at lm12-laptop02 ~/Projects $ python3 test_script2.py
> Bit Length: 140
> 0b10101000110010101110011011101000110100101101110011001110010000001011000010011110101001000100000010000010110011101100001011010010110111000100001
> 7351954002991226380810260999848996570230305
> Traceback (most recent call last):
> File "test_script.py", line 24, in <module>
> data = binascii.unhexlify(bytes(data, 'utf8'))
> OverflowError: cannot fit 'int' into an index-sized integer
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list