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
> 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
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,
> 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
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
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.
Steven, one piece of advice.
Python is not C.
Thank.
cheers
James
Ok, I will study all your advices. I think I may find my way when
doing real tasks.
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
> 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
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.
Predict the future, of course.
--
Grant Edwards grante Yow! MERYL STREEP is my
at obstetrician!
visi.com
[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
Good suggestion, thanks!