read string in bits

T

ts

hi, is there a way to read a character/string into bits in python?

i understand that character is read in bytes. Do i have to write a
function to convert it myself into 1010101 or there is a library in
python that enable me to do that?
 
C

Chris Rebert

hi, is there a way to read a character/string into bits in python?

i understand that character is read in bytes. Do i have to write a
function to convert it myself into 1010101 or there is a library in
python that enable me to do that?

It's not quite clear to me what you mean, but here are 2 guesses:
- If you want to convert an ASCII character to its ASCII integer
value, use ord()
- If you want to convert an integer into a string of its base-2
representation, use bin() [requires Python 2.6, I think]

Cheers,
Chris
 
T

ts

hi, is there a way to read a character/string into bits in python?
i understand that character is read in bytes. Do i have to write a
function to convert it myself into 1010101 or there is a library in
python that enable me to do that?

It's not quite clear to me what you mean, but here are 2 guesses:
- If you want to convert an ASCII character to its ASCII integer
value, use ord()
- If you want to convert an integer into a string of its base-2
representation, use bin() [requires Python 2.6, I think]

Cheers,
Chris

hi, bin() is what i'm looking for. But only python 2.4 is available to
me. Is there a replacement of bin() in python 2.4?
 
H

Hendrik van Rooyen

Chris Rebert said:
It's not quite clear to me what you mean, but here are 2 guesses:
- If you want to convert an ASCII character to its ASCII integer
value, use ord()
- If you want to convert an integer into a string of its base-2
representation, use bin() [requires Python 2.6, I think]

Another case:

Works almost anywhere, AFAIK.

- Hendrik
 
J

John Machin

It's not quite clear to me what you mean, but here are 2 guesses:
- If you want to convert an ASCII character to its ASCII integer
value, use ord()
- If you want to convert an integer into a string of its base-2
representation, use bin() [requires Python 2.6, I think]
Cheers,
Chris

hi, bin() is what i'm looking for. But only python 2.4 is available to
me. Is there a replacement of bin() in python 2.4?

No. You would have to write some code 8-(
This should give you some clues:

| Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit
(Intel)] on win32
| Type "help", "copyright", "credits" or "license" for more
information.
| >>> def char_as_number(char, base):
| ... assert 2 <= base <= 16
| ... n = ord(char)
| ... if not n:
| ... return '0'
| ... result = ''
| ... while n:
| ... n, r = divmod(n, base)
| ... result = '0123456789ABCDEF'[r] + result
| ... return result
| ...
| >>> [char_as_number(chr(x), 2) for x in (0, 1, 7, 8, 127, 128, 255)]
| ['0', '1', '111', '1000', '1111111', '10000000', '11111111']
| >>> [char_as_number(chr(x), 2).zfill(8) for x in (0, 1, 7, 8, 127,
128, 255)]
| ['00000000', '00000001', '00000111', '00001000', '01111111',
'10000000', '11111111']
| >>> [char_as_number(chr(x), 16).zfill(2) for x in (0, 1, 7, 8, 127,
128, 255)]
| ['00', '01', '07', '08', '7F', '80', 'FF']
| >>> [char_as_number(chr(x), 8).zfill(3) for x in (0, 1, 7, 8, 127,
128, 255)]
| ['000', '001', '007', '010', '177', '200', '377']
| >>>

HTH,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top