Re: How to represent a sequence of raw bytes
On Mon, Dec 22, 2008 at 4:56 PM, Steven Woody narkewo...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to represent a sequence of raw bytes
On Mon, Dec 22, 2008 at 4:35 PM, James Mills prolo...@shortcircuit.net.au wrote: On Mon, Dec 22, 2008 at 4:56 PM, Steven Woody narkewo...@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 Ok, I will study all your advices. I think I may find my way when doing real tasks. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to represent a sequence of raw bytes
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
Re: How to represent a sequence of raw bytes
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to represent a sequence of raw bytes
On Mon, Dec 22, 2008 at 6:44 PM, Steven D'Aprano st...@remove-this-cybersource.com.au wrote: 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? 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to represent a sequence of raw bytes
On 2008-12-22, Steven D'Aprano st...@remove-this-cybersource.com.au wrote: 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? Predict the future, of course. -- Grant Edwards grante Yow! MERYL STREEP is my at obstetrician! visi.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to represent a sequence of raw bytes
On Dec 23, 1:52 am, Steven Woody narkewo...@gmail.com wrote: On Mon, Dec 22, 2008 at 6:44 PM, Steven D'Aprano st...@remove-this-cybersource.com.au wrote: 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? 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. [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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to represent a sequence of raw bytes
On Tue, Dec 23, 2008 at 5:05 AM, John Machin sjmac...@lexicon.net wrote: On Dec 23, 1:52 am, Steven Woody narkewo...@gmail.com wrote: On Mon, Dec 22, 2008 at 6:44 PM, Steven D'Aprano st...@remove-this-cybersource.com.au wrote: 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? 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. [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! -- http://mail.python.org/mailman/listinfo/python-list
How to represent a sequence of raw bytes
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to represent a sequence of raw bytes
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to represent a sequence of raw bytes
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, -- http://mail.python.org/mailman/listinfo/python-list
Re: How to represent a sequence of raw bytes
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 smime.p7s Description: S/MIME Cryptographic Signature -- http://mail.python.org/mailman/listinfo/python-list
Re: How to represent a sequence of raw bytes
On Sun, Dec 21, 2008 at 10:56 PM, Steven Woody narkewo...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to represent a sequence of raw bytes
Steven Woody narkewo...@gmail.com writes: 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? 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. -- http://mail.python.org/mailman/listinfo/python-list