[英]Reading C comma separated hex file into python
I would like to read a C Comma separated array file into a python list and do some operations on the data before sending it to another file. 我想将C逗号分隔的数组文件读取到python列表中,并对数据进行一些操作,然后再将其发送到另一个文件。 Any ideas on how to do this while ignoring the comments ?
关于忽略注释如何执行此操作的任何想法?
// Example file data.h
unsigned const short font_5x7[480] = {
0x00,0x00,0x00,0x00,0x00, //
0x00,0x00,0xfa,0x00,0x00, // !
0x00,0xe0,0x00,0xe0,0x00, // "
0x28,0xfe,0x28,0xfe,0x28, // #
0x24,0x54,0xfe,0x54,0x48, // $
0xc4,0xc8,0x10,0x26,0x46, // %
etc.....
Use a regex like this 使用这样的正则表达式
(?<data>[^/]*?)[ ]*//[^\n]*([\n\r]|())
Now in the "data" group there is essentialy this 现在在“数据”组中,必不可少的是
0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xfa,0x00,0x00,
0x00,0xe0,0x00,0xe0,0x00,
0x28,0xfe,0x28,0xfe,0x28,
0x24,0x54,0xfe,0x54,0x48,
0xc4,0xc8,0x10,0x26,0x46,
You just need to itterate through all the matches and paste it in a string 您只需要在所有匹配项中完成,然后将其粘贴到字符串中即可
If you are in a position to access the code and a C compiler it may be easier to write a small C program to output the data in a nicer format: 如果您可以访问代码和C编译器,则编写较小的C程序以更好的格式输出数据可能会更容易:
#include <stdio.h>
#include "data.h"
int main(int argc, char *argv[])
{
int i;
for (i=0; i<480; i++)
printf("%i\n", font_5x7[i]);
return 0;
}
use regex 使用正则表达式
(0x\d+),
to capture hex number globally. 全局捕获十六进制数。
then 然后
int(hex_num, 16)
to turn them into integer. 把它们变成整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.