l*****r 发帖数: 15615 | 1 in a communication task, the python script received the data buff as a list
of bytes.
since python tool was used to do the communication with embedded software,
which were written in C/C++;
so the message were definded in c as type of structure, for example
struct
{
unsigned long variable1;
unsigned char variable2;
unsigned char variable3[4];
...
}MessageType
in stead of writing a function in python to decode the message buffer byte
by byte: forming four bytes into unsigned long/int; and etc.
is there a better approach for this? | j*a 发帖数: 14423 | 2 http://docs.python.org/py3k/library/index.html
6.3. struct — Interpret bytes as packed binary data
list
【在 l*****r 的大作中提到】 : in a communication task, the python script received the data buff as a list : of bytes. : since python tool was used to do the communication with embedded software, : which were written in C/C++; : so the message were definded in c as type of structure, for example : struct : { : unsigned long variable1; : unsigned char variable2; : unsigned char variable3[4];
| l*****r 发帖数: 15615 | 3 how does that gonna help?
I already have the data in bytes in a list.
let us say the data is like
abc = [ ,,,,,0x54, 0xA1, 0xB2, 0x3F]
and the last 4 bytes of the data is a number as: 0x3FB2A154, since they were
in little Endian. how does struct can help in this case.
Thanks for your reply.
【在 j*a 的大作中提到】 : http://docs.python.org/py3k/library/index.html : 6.3. struct — Interpret bytes as packed binary data : : list
| j*a 发帖数: 14423 | 4 http://docs.python.org/py3k/library/struct.html#examples
were
【在 l*****r 的大作中提到】 : how does that gonna help? : I already have the data in bytes in a list. : let us say the data is like : abc = [ ,,,,,0x54, 0xA1, 0xB2, 0x3F] : and the last 4 bytes of the data is a number as: 0x3FB2A154, since they were : in little Endian. how does struct can help in this case. : Thanks for your reply.
| l*****r 发帖数: 15615 | 5 apperantly, I am not that smart as you. and don't see how examples helps me.
【在 j*a 的大作中提到】 : http://docs.python.org/py3k/library/struct.html#examples : : were
| l*****r 发帖数: 15615 | 6 well, you could explain better just saying somthing like
pack the list into binary then unpack them;
but throwing out web links, didn't really help.
and you have more WB than me, so I guess you will be fine if I don't pay you
any.
【在 j*a 的大作中提到】 : http://docs.python.org/py3k/library/struct.html#examples : : were
| j*a 发帖数: 14423 | 7 that's cool
you
【在 l*****r 的大作中提到】 : well, you could explain better just saying somthing like : pack the list into binary then unpack them; : but throwing out web links, didn't really help. : and you have more WB than me, so I guess you will be fine if I don't pay you : any.
|
|