read string in bits

Discussion in 'Python' started by ts, Jan 14, 2009.

  1. ts

    ts Guest

    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?
    ts, Jan 14, 2009
    #1
    1. Advertising

  2. ts

    Chris Rebert Guest

    On Tue, Jan 13, 2009 at 11:21 PM, ts <> 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
    Chris Rebert, Jan 14, 2009
    #2
    1. Advertising

  3. ts

    ts Guest

    On Jan 14, 3:32 pm, Chris Rebert <> wrote:
    > On Tue, Jan 13, 2009 at 11:21 PM, ts <> 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?
    ts, Jan 14, 2009
    #3
  4. "Chris Rebert" <> 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
    Hendrik van Rooyen, Jan 14, 2009
    #4
  5. ts

    John Machin Guest

    On Jan 14, 6:44 pm, ts <> wrote:
    > On Jan 14, 3:32 pm, Chris Rebert <> wrote:
    >
    >
    >
    > > On Tue, Jan 13, 2009 at 11:21 PM, ts <> 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(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
    John Machin, Jan 14, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GGG
    Replies:
    10
    Views:
    12,556
    Donar
    Jul 6, 2006
  2. sarmin kho
    Replies:
    2
    Views:
    826
    A. Lloyd Flanagan
    Jun 15, 2004
  3. Miki Tebeka
    Replies:
    1
    Views:
    445
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
  4. sergey

    "casting" bits to bits?

    sergey, Nov 8, 2006, in forum: VHDL
    Replies:
    1
    Views:
    706
    sergey
    Nov 8, 2006
  5. Tomás

    Value Bits Vs Object Bits

    Tomás, Jun 2, 2006, in forum: C Programming
    Replies:
    13
    Views:
    547
    Hallvard B Furuseth
    Jul 1, 2006
Loading...

Share This Page