How to Convert a string into binary
HNT20
hnt20 at msn.com
Sat Apr 15 17:44:26 EDT 2006
Rune Strand wrote:
> HNT20 wrote:
>> Hello All
>>
>
> def ascii_to_bin(char):
> ascii = ord(char)
> bin = []
>
> while (ascii > 0):
> if (ascii & 1) == 1:
> bin.append("1")
> else:
> bin.append("0")
> ascii = ascii >> 1
>
> bin.reverse()
> binary = "".join(bin)
> zerofix = (8 - len(binary)) * '0'
>
> return zerofix + binary
>
>
>
> some_string = 'Time to go now, Rummy?'
>
> binary = []
> for char in some_string:
> binary.append(ascii_to_bin(char))
>
> print binary
> print " ".join(binary)
>
>
>
> some_string = 'Time to go now, Rummy?'
>
> binary = []
> for char in some_string:
> binary.append(ascii_to_bin(char))
>
> print binary
> print " ".join(binary)
>
> """
> ['01010100', '01101001', '01101101', '01100101', '00100000',
> '01110100', '01101111', '00100000', '01100111', '01101111', '00100000',
> '01101110', '01101111', '01110111', '00101100', '00100000', '01010010',
> '01110101', '01101101', '01101101', '01111001', '00111111']
> 01010100 01101001 01101101 01100101 00100000 01110100 01101111 00100000
> 01100111 01101111 00100000 01101110 01101111 01110111 00101100 00100000
> 01010010 01110101 01101101 01101101 01111001 00111111
> """
>
Thanks very much. this code looks really promising. i will try to
implement this code and see if i get any luck
HNT
More information about the Python-list
mailing list