<br><br>
<div class="gmail_quote">On Thu, Jul 19, 2012 at 12:16 AM, Dave Angel <span dir="ltr">&lt;<a href="mailto:d@davea.name" target="_blank">d@davea.name</a>&gt;</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>&gt; OK so I have been trying for a couple days now and I am throwing in the<br>&gt; towel, Python 3 wins this one.<br>&gt; I want to convert a string to binary and back again like in this<br>
&gt; question: Stack Overflow: Convert Binary to ASCII and vice versa<br>&gt; (Python)<br>&gt; &lt;<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>&gt;<br>
&gt; But in Python 3 I consistently get  some sort of error relating to the<br>&gt; fact that nothing but bytes and bytearrays support the buffer interface<br>&gt; or I get an overflow error because something is too large to be<br>
&gt; converted to bytes.<br>&gt; Please help me and then explian what I am not getting that is new in<br>&gt; Python 3. I would like to point out I realize that binary, hex, and<br>&gt; encodings are all a very complex subject and so I do not expect to<br>
&gt; master it but I do hope that I can gain a deeper insight. Thank you all.<br>&gt;<br>&gt; test_script.py:<br>&gt; import binascii<br>&gt;<br>&gt; test_int = 109<br>&gt;<br>&gt; test_int = int(str(test_int) + &#39;45670&#39;)<br>
&gt; data = &#39;Testing XOR Again!&#39;<br>&gt;<br>&gt; while sys.getsizeof(data) &gt; test_int.bit_length():<br>&gt;<br>&gt; test_int = int(str(test_int) + str(int.from_bytes(os.urandom(1), &#39;big&#39;)))<br>&gt;<br>&gt; print(&#39;Bit Length: &#39; + str(test_int.bit_length()))<br>
&gt;<br>&gt; key = test_int # Yes I know this is an unnecessary step...<br>&gt;<br>&gt; data = bin(int(binascii.hexlify(bytes(data, &#39;UTF-8&#39;)), 16))<br>&gt;<br>&gt; print(data)<br>&gt;<br>&gt; data = int(data, 2)<br>
&gt;<br>&gt; print(data)<br>&gt;<br>&gt; data = binascii.unhexlify(&#39;%x&#39; % data)<br>&gt;<br><br></div></div>I don&#39;t get the same error you did.  I get:<br><br> File &quot;jordan.py&quot;, line 13<br>
<div class="im">    test_int = int(str(test_int) + str(int.from_bytes(os.urandom(1),<br>&#39;big&#39;)))<br></div>           ^<br></blockquote>
<div>test_int = int(str(test_int) + str(int.from_bytes(os.urandom(1), \<br>    &#39;big&#39;)))</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&#39;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&#39;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&#39;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( &quot;%x&quot; % data, &quot;ascii&quot;)<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>