On 22 Dic, 03:23, "Steven Woody" <narkewo...@gmail.com> wrote: > 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
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 -- http://mail.python.org/mailman/listinfo/python-list