<br><br>
<div class="gmail_quote">On Thu, Jul 19, 2012 at 12:16 AM, Dave Angel <span dir="ltr"><<a href="mailto:d@davea.name" target="_blank">d@davea.name</a>></span> wrote:<br>
<blockquote style="BORDER-LEFT:#ccc 1px solid;MARGIN:0px 0px 0px 0.8ex;PADDING-LEFT:1ex" class="gmail_quote">
<div class="HOEnZb">
<div class="h5">On 07/18/2012 05:07 PM, Jordan wrote:<br>> OK so I have been trying for a couple days now and I am throwing in the<br>> towel, Python 3 wins this one.<br>> I want to convert a string to binary and back again like in this<br>
> question: Stack Overflow: Convert Binary to ASCII and vice versa<br>> (Python)<br>> <<a href="http://stackoverflow.com/questions/7396849/convert-binary-to-ascii-and-vice-versa-python" target="_blank">http://stackoverflow.com/questions/7396849/convert-binary-to-ascii-and-vice-versa-python</a>><br>
> But in Python 3 I consistently get some sort of error relating to the<br>> fact that nothing but bytes and bytearrays support the buffer interface<br>> or I get an overflow error because something is too large to be<br>
> converted to bytes.<br>> Please help me and then explian what I am not getting that is new in<br>> Python 3. I would like to point out I realize that binary, hex, and<br>> encodings are all a very complex subject and so I do not expect to<br>
> master it but I do hope that I can gain a deeper insight. Thank you all.<br>><br>> test_script.py:<br>> import binascii<br>><br>> test_int = 109<br>><br>> test_int = int(str(test_int) + '45670')<br>
> data = 'Testing XOR Again!'<br>><br>> while sys.getsizeof(data) > test_int.bit_length():<br>><br>> test_int = int(str(test_int) + str(int.from_bytes(os.urandom(1), 'big')))<br>><br>> print('Bit Length: ' + str(test_int.bit_length()))<br>
><br>> key = test_int # Yes I know this is an unnecessary step...<br>><br>> data = bin(int(binascii.hexlify(bytes(data, 'UTF-8')), 16))<br>><br>> print(data)<br>><br>> data = int(data, 2)<br>
><br>> print(data)<br>><br>> data = binascii.unhexlify('%x' % data)<br>><br><br></div></div>I don't get the same error you did. I get:<br><br> File "jordan.py", line 13<br>
<div class="im"> test_int = int(str(test_int) + str(int.from_bytes(os.urandom(1),<br>'big')))<br></div> ^<br></blockquote>
<div>test_int = int(str(test_int) + str(int.from_bytes(os.urandom(1), \<br> 'big')))</div>
<div># That was probably just do to the copy and paste.<br></div>
<blockquote style="BORDER-LEFT:#ccc 1px solid;MARGIN:0px 0px 0px 0.8ex;PADDING-LEFT:1ex" class="gmail_quote">IndentationError: expected an indented block<br><br><br>Please post it again, with correct indentation. if you used tabs, then<br>
expand them to spaces before pasting it into your test-mode mail editor.<br><br></blockquote>
<div>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.</div>
<blockquote style="BORDER-LEFT:#ccc 1px solid;MARGIN:0px 0px 0px 0.8ex;PADDING-LEFT:1ex" class="gmail_quote"><br>I'd also recommend you remove a lot of the irrelevant details there. if<br>you have a problem with hexlfy and/or unhexlify, then give a simple byte<br>
string that doesn't work for you, and somebody can probably identify why<br>not. And if you want people to run your code, include the imports as well.<br><br></blockquote>
<div>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. </div>
<div>Sorry that I forgot to include sys and os for imports.</div>
<div> </div>
<blockquote style="BORDER-LEFT:#ccc 1px solid;MARGIN:0px 0px 0px 0.8ex;PADDING-LEFT:1ex" class="gmail_quote">As it is, you're apparently looping, comparing the byte memory size of a<br>string (which is typically 4 bytes per character) with the number of<br>
significant bits in an unrelated number.<br><br>I suspect what you want is something resembling (untested):<br><br> mybytes = bytes( "%x" % data, "ascii")<br> newdata = binascii.unexlify(mybytes)<br>
<span class="HOEnZb"><font color="#888888"><br></font></span></blockquote>
<div>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.</div>
<blockquote style="BORDER-LEFT:#ccc 1px solid;MARGIN:0px 0px 0px 0.8ex;PADDING-LEFT:1ex" class="gmail_quote"><span class="HOEnZb"><font color="#888888"><br>--<br>DaveA<br></font></span></blockquote></div><br>