Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > read string in bits

Reply
Thread Tools

read string in bits

 
 
ts
Guest
Posts: n/a
 
      01-14-2009
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?
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      01-14-2009
On Tue, Jan 13, 2009 at 11:21 PM, ts <(E-Mail Removed)> wrote:
> 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

--
Follow the path of the Iguana...
http://rebertia.com
 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      01-14-2009
On Jan 14, 3:32*pm, Chris Rebert <(E-Mail Removed)> wrote:
> On Tue, Jan 13, 2009 at 11:21 PM, ts <(E-Mail Removed)> wrote:
> > 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
>
> --
> Follow the path of the Iguana...http://rebertia.com


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?
 
Reply With Quote
 
Hendrik van Rooyen
Guest
Posts: n/a
 
      01-14-2009
"Chris Rebert" <(E-Mail Removed)> wrote:

> 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:

>>> s = '1010101'
>>> int (s,2)

85
>>>


Works almost anywhere, AFAIK.

- Hendrik

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      01-14-2009
On Jan 14, 6:44*pm, ts <(E-Mail Removed)> wrote:
> On Jan 14, 3:32*pm, Chris Rebert <(E-Mail Removed)> wrote:
>
>
>
> > On Tue, Jan 13, 2009 at 11:21 PM, ts <(E-Mail Removed)> wrote:
> > > 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

>
> > --
> > Follow the path of the Iguana...http://rebertia.com

>
> 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( 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), .zfill(3) for x in (0, 1, 7, 8, 127,
128, 255)]
| ['000', '001', '007', '010', '177', '200', '377']
| >>>

HTH,
John
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shifting bits, shift 32 bits on 32 bit int GGG C++ 10 07-06-2006 06:09 AM
what about unsigned and signed 8 bits number, 16 bits, etc?? sarmin kho Python 2 06-15-2004 06:40 PM
8 bits/ch vs 16 bits/ch in PS Terry Digital Photography 5 01-21-2004 06:59 PM
8-Bits vs 12 or 16 bits/pixel; When does more than 8 bits count ? Al Dykes Digital Photography 3 12-29-2003 07:08 PM
win XP 32 bits on a 64 bits processor.. Abbyss Computer Support 3 11-13-2003 12:39 AM



Advertisments