> > However, as you keep claiming that the struct module is what > should be used, I must be missing something about the struct > module.
You seem to be focusing overly on the "C Struct" part of the description of what the struct module does, instead of the part where it says, "packed binary data" and "This can be used in handling binary data stored in files or from network connections" The struct module is very useful when generating or interpreting a stream of binary data which has a set structured format, in bytes and with set kinds of data types. > > # I have following packet format which I have to send over Bluetooth. > # packet_type (1 byte unsigned) || packet_length (1 byte unsigned) || > # packet_data(variable) > > So yes, all his date is comprised of 8-bit bytes, and yes, he doesn't > need the struct module. Hence I'm puzzled why people suggest that > he uses the struct module. > > I think the key answer is "use the string type, it is appropriate > to represent byte oriented data in python" (also see the subject > of this thread) > Everyone *is* saying to use the string type: the struct module doesn't produce structs.. it produces strings. The question is how to convert from Python types to byte-representations of said types in a string: you are saying (for example) that for shorts someone should use chr(1) etc. What if he needs to then send a two byte int from a Python int? Just because he said his message was about a fairly simple requirement at the moment, it doesn't mean that's the end of his problem. Everyone else seems to have assumed that it'll advance as he supports more of this protocol: that <type><length><variable data> is apt to take on more specific struct format messages based on <type>. So you might have a connection request format that's pre-defined -- your example above -- or another which needs to pass say, a 2 byte int instead. The struct module easily accomplishes this: all together in one place. Instead of having to figure out how to calculate the 2 byte storage of an int down the road, or a 4 byte integer, etc. Not to mention endianness. If the OP never ever has to do anything but the one specific problem he's having right now (and this isn't just an example he's making) with really really simple data: two byte-ints and then some characters, then sure using chr() and string concatenation is probably fine. But struct works -perfectly- fine for variable data and as he extends his support for the protocol, is quite likely to be very useful to him: that's why we're suggesting it. IMHO. I'd rather spend some trival time using string concatination (or the % operator) generating format strings for struct then have to write code for "int_to_2byte_int" and "int_to_4byte_int" and "change_to_bigendian" for when chr() doesn't satisfy the conversation needs enough. Yeah, strings are exactly how he can and should store the byte oriented data in Python 2.x. Using concatination and slicing + conversion to read and parse a byte stream coming off a line protocol... if you want to. But there's tools that are there that make it all alot easier too. The struct module for example. --Stephen
-- http://mail.python.org/mailman/listinfo/python-list