How to represent a sequence of raw bytes

Chris Rebert clp at rebertia.com
Mon Dec 22 02:08:32 EST 2008


On Sun, Dec 21, 2008 at 10:56 PM, Steven Woody <narkewoody at gmail.com> wrote:
> On Mon, Dec 22, 2008 at 10:27 AM, Michiel Overtoom <motoom at xs4all.nl> wrote:
>> On Monday 22 December 2008 03:23:03 Steven Woody wrote:
>>
>>> 2.  char buf[] = {0x11, 0x22, 0x33, ... }
>>>
>>> What's the equivalent representation for above in Python?
>>
>>>>> buf="\x11\x22\33"
>>>>> for b in buf: print ord(b)
>> ...
>> 17
>> 34
>> 27
>>>>>
>>
>
> Hi, Michiel
>
> I thing "\x11\x22\x33" in python is not the {0x11, 0x22, 0x33} in C.
> Since, a string in python is immutable, I can _not_ do something like:
>  b[1] = "\x55".
>
> And, how about char buf[200] in my original question?  The intension
> is to allocate 200 undefined bytes in memory. Thanks.

You want the `bytearray` type referred to in PEP 3137
(http://www.python.org/dev/peps/pep-3137/).
However, I believe `bytearray` is only available in Python 3.0

Cheers,
Chris

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



More information about the Python-list mailing list