[Tutor] string to binary and back... Python 3
Prasad, Ramit
ramit.prasad at jpmorgan.com
Thu Jul 19 00:22:43 CEST 2012
>
> I think your basic problem is too much conversion because you do not
> understand the types. A string is represented by a series of bytes
> which are binary numbers. Do you understand the concept behind ASCII?
> Each letter has a numeric representation that are sequential.
> So the string 'abcd' is equivalent to a series of bytes 65,66,67,68.
> It is not equivalent to 65666768 or 65+66+67+68. So your first
> task is to convert each character to the numeric equivalent and store
> them in a list. Once you have them converted to a list of integers,
> you can create another list that is a list of characters.
>
> Look at the functions chr and ord here
> ( http://docs.python.org/py3k/library/functions.html )
I forgot to say, that once you have the integer equivalents,
you can then convert that easily to binary using bin.
I used ast.literal_eval to convert from binary string
(as returned from bin) to number, but there might be better
ways.
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.
More information about the Tutor
mailing list