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

wolfrage8765 at gmail.com wolfrage8765 at gmail.com
Thu Jul 19 07:41:42 CEST 2012


On Thu, Jul 19, 2012 at 12:16 AM, Dave Angel <d at davea.name> wrote:

>  On 07/18/2012 05:07 PM, Jordan wrote:
> > 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)
> >
>
> I don't get the same error you did.  I get:
>
>  File "jordan.py", line 13
>     test_int = int(str(test_int) + str(int.from_bytes(os.urandom(1),
> 'big')))
>            ^
>
test_int = int(str(test_int) + str(int.from_bytes(os.urandom(1), \
    'big')))
# That was probably just do to the copy and paste.

> IndentationError: expected an indented block
>
>
> Please post it again, with correct indentation.  if you used tabs, then
> expand them to spaces before pasting it into your test-mode mail editor.
>
> I only use spaces and this program did not require any indentation until
it was pasted and the one line above became split across two line. Really
though that was a trivial error to correct.

>
> I'd also recommend you remove a lot of the irrelevant details there.  if
> you have a problem with hexlfy and/or unhexlify, then give a simple byte
> string that doesn't work for you, and somebody can probably identify why
> not.  And if you want people to run your code, include the imports as well.
>
> My problem is not specific to hexlify and unhexlify, my problem is trying
to convert from string to binary and back. That is why all of the details,
to show I have tried on my own.
Sorry that I forgot to include sys and os for imports.


> As it is, you're apparently looping, comparing the byte memory size of a
> string (which is typically 4 bytes per character) with the number of
> significant bits in an unrelated number.
>
> I suspect what you want is something resembling (untested):
>
>     mybytes = bytes( "%x" % data, "ascii")
>     newdata = binascii.unexlify(mybytes)
>
> I was comparing them but I think I understand how to compare them well,
now I want to convert them both to binary so that I can XOR them together.
Thank you for your time and help Dave, now I need to reply to Ramit.

>
> --
> DaveA
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120719/ece37ad0/attachment-0001.html>


More information about the Tutor mailing list