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

Jordan wolfrage8765 at gmail.com
Thu Jul 19 22:58:16 CEST 2012


Sorry, I am not sure why Thunderbird is stripping the spaces, may have
something to do with a plug-in that I have installed, I will have to
look into it.

On 07/19/2012 10:41 PM, Prasad, Ramit wrote:
> Sure, this makes perfect sense to me :) (adding indent)
>
> for char in data:
>     bin_data += bin(ord(char)).split('b')[1].zfill(8)
> bin_list = [bin_data[x:x + 2] for x in range(0, len(bin_data), 2)]
>
> Why are you grabbing 2 binary digits? The only possibilities are 0,1,2,3
> and none are ASCII letters. You should be grabbing 8 at a time.
Right, sorry, first time working with binary and I was confused by a
previous attempt.
>
> bin_data = [ bin(ord(char)).split('b')[1].zfill(8) for char in data ]
> bin_string = ''.join(bin_data)
> bin_list = [ chr( int(char, 2) ) for char in bin_data ]
Thank you exactly what I was looking for!
>
> I am not really sure what you are getting at with XOR and one time
> padding, but it has been a while since I have done any encryption.
And I have just started reading "Applied Cryptography", so I am putting
some of what I learn into practice.
>
> I would think you could do all this by just converting everything
> to int and then adding/replacing the pad in the list of ints.
At first I was essentially doing just that, but when I first converted
the large integers that are being used for the one time pad as the key
to binary I saw how much larger it was, and then realized that was the
bit length of the integer (technically Long). By doing that, I can get
more out of the one time pad, but if you XOR binary against Ord, very
few values will be changed because binary is only 1s and 0s as you know.
To optimize the keys use, whether it wastes memory or not, I wanted to
use binary on binary, this really comes into play with files, not so
much the shorter strings.
But since you bring if up, how would you convert a file to a list of ints?
>
>
> Ramit
>
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.  
> _______________________________________________
> 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