Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to represent a sequence of raw bytes

6 views
Skip to first unread message

Steven Woody

unread,
Dec 21, 2008, 9:23:03 PM12/21/08
to python-list
Hi,

What's the right type to represent a sequence of raw bytes. In C, we usually do

1. char buf[200] or
2. char buf[] = {0x11, 0x22, 0x33, ... }

What's the equivalent representation for above in Python?

Thanks.

-
narke

Michiel Overtoom

unread,
Dec 21, 2008, 9:27:36 PM12/21/08
to pytho...@python.org
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
>>>


Greetings,

--
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Vallopillil
http://www.catb.org/~esr/halloween/halloween4.html

Steven Woody

unread,
Dec 22, 2008, 1:56:45 AM12/22/08
to Michiel Overtoom, pytho...@python.org
On Mon, Dec 22, 2008 at 10:27 AM, Michiel Overtoom <mot...@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.

Regards,

Tino Wildenhain

unread,
Dec 22, 2008, 2:06:53 AM12/22/08
to Steven Woody, pytho...@python.org
Steven Woody wrote:
> On Mon, Dec 22, 2008 at 10:27 AM, Michiel Overtoom <mot...@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"
...

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

Well in most cases you don't need to do that, instead you could assemble
your stream on the way out based on sequences, generators etc.

Please note that char in your example is just a bit inapropriate (but
common in C) shorthand for unsigned short int. There is no such type
in python so you could use int() if you want to operate on the numeric
value. Depending on your use case a big integer could also serve well
and you can convert it into a byte string.

If you want random access to the bytes, you can use list or array (see
array module) or, if you want it with much more performance resort
to numpy, scipy, they have arrays similar to C and also much more
numeric datatypes.

Regards
Tino

Chris Rebert

unread,
Dec 22, 2008, 2:08:32 AM12/22/08
to Steven Woody, pytho...@python.org
On Sun, Dec 21, 2008 at 10:56 PM, Steven Woody <narke...@gmail.com> wrote:
> On Mon, Dec 22, 2008 at 10:27 AM, Michiel Overtoom <mot...@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

Hrvoje Niksic

unread,
Dec 22, 2008, 2:40:40 AM12/22/08
to
"Steven Woody" <narke...@gmail.com> writes:

import array
buf = array.array('b', [0x11, 0x22, ...])

It automatically retrieves byte values without having to call ord(),
and it allows changing them. It also has a C API for getting to the
address of the underlying buffer.

James Mills

unread,
Dec 22, 2008, 3:35:30 AM12/22/08
to Steven Woody, pytho...@python.org
On Mon, Dec 22, 2008 at 4:56 PM, Steven Woody <narke...@gmail.com> wrote:
> 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.

Steven, one piece of advice.

Python is not C.

Thank.

cheers
James

Steven Woody

unread,
Dec 22, 2008, 4:11:18 AM12/22/08
to James Mills, pytho...@python.org

Ok, I will study all your advices. I think I may find my way when
doing real tasks.

bief...@gmail.com

unread,
Dec 22, 2008, 4:57:12 AM12/22/08
to

Usually, if I have to manipulate bytes (e.g. computing checksum,
etc...) i just use a list of numbers:

buf = [11, 22, 33, ...]

then when I need to put it in a buffer similar to the one in C (e.g.
before sending a packet of bytes through a socket
or another I/O channel), I use struct.pack

import struct
packed_buf = struct.pack('B'*len(buf), buf )

similarly, if I get a packet of bytes from an I/O channel and I need
to do operation on them as single bytes, I do:

buf = struct.unpack('B'*len(packed_buf), packed_buf )

Note that struct.pack and struct.unpack can trasform packed bytes in
other kind of data, too ...

There are other - maybe more efficient - way of handling bytes in
python programs, like using array as already suggested, but, up
to now, I never needed them in my python programs, which are not real-
time stuff, but sometime need to process steady flows of
data.

Ciao
----
FB

Steven D'Aprano

unread,
Dec 22, 2008, 5:44:39 AM12/22/08
to
On Mon, 22 Dec 2008 14:56:45 +0800, Steven Woody wrote:

> The intension is to allocate 200 undefined bytes in memory.

You *want* undefined bytes? Out of curiosity, what do you intend to do
with them?


--
Steven

Steven Woody

unread,
Dec 22, 2008, 9:52:30 AM12/22/08
to Steven D'Aprano, pytho...@python.org

to receive/send network packets, read raw files, etc. After read
replies of the thread, I think 'array' or 'struct' maybe what I
wanted, may a plain list can do, but I am not sure.

Grant Edwards

unread,
Dec 22, 2008, 10:45:24 AM12/22/08
to

Predict the future, of course.

--
Grant Edwards grante Yow! MERYL STREEP is my
at obstetrician!
visi.com

John Machin

unread,
Dec 22, 2008, 4:05:46 PM12/22/08
to

[Rhetorical questions] How do you use "undefined bytes" to receive
network packets?? Do you really want to use "undefined bytes" to
*send* network packets????

Suggestion: Stop trying to replicate C in Python; think about what you
are trying to accomplish at a higher level, then how to implement that
in Python.

Cheers,
John

Steven Woody

unread,
Dec 22, 2008, 7:41:28 PM12/22/08
to John Machin, pytho...@python.org

Good suggestion, thanks!

0 new messages