在学习和工作开发的时候,经常需要使用到各种各样不太常用的操作,这种情况一般是自己手动写一些小程序来处理。因为它们不太常用,所以经常用了又没保存,等到下一次在使用的时候又需要重写,这样的非常浪费时间和精力。
所以想在这里统一记录一下,以备下次重新使用。代码以实用为主,如果缺陷,欢迎指出。
1、十六进制字符转整型数字
功能:将16进制的字符串转换为10进制的数字。我是没有找到相应的库函数,所以参考网上的代码自己手动写了个函数来实现。
常用的函数有atoi,atol,他们都是将10进制的数字字符串转换为int或是long类型,所以在有些情况下不适用。
/*=============================================================================# filename: hex2dec.cpp# desc: convert a hex string to a int number# author: caibiao lee# version: # lastchange: 2018-11-26 # history:=============================================================================*/ #include #include #include #include int c2i(char ch) { // 如果是数字,则用数字的ascii码减去48, 如果ch = '2' ,则 '2' - 48 = 2 if(isdigit(ch)) return ch - 48; // 如果是字母,但不是a~f,a~f则返回 if( ch 'f' && ch 'z' ) return -1; // 如果是大写字母,则用数字的ascii码减去55, 如果ch = 'a' ,则 'a' - 55 = 10 // 如果是小写字母,则用数字的ascii码减去87, 如果ch = 'a' ,则 'a' - 87 = 10 if(isalpha(ch)) return isupper(ch) ? ch - 55 : ch - 87; return -1; } int hex2dec(char *hex) { int len; int num = 0; int temp; int bits; int i; char str[64] = {0}; if(null==hex) { printf(input para error ); return 0; } if(('0'==hex[0])&&(('x'==hex[1])||('x'==hex[1]))) { strcpy(str,&hex[2]); }else { strcpy(str,hex); } printf(input num = %s ,str); // 此例中 str = 1de 长度为3, hex是main函数传递的 len = strlen(str); for (i=0, temp=0; i
=(len=strlen((const char *)pstr))) return -1; for(i=(len-1);i>=0;i--) { if(pstr[i]>'f') { for(t=0;t3、创建文件并填充固定数据
功能:创建固定大小的一个文件,并且把这个文件填充为固定的数据。
/*=============================================================================# filename: createfile.cpp# desc: 创建固定大小的文件,然后填充固定的数据# author: caibiao lee# version: # lastchange: 2018-11-26 # history:=============================================================================*/#include #include #include #include //#define fill_data_value 0xff#define fill_data_value 0x30 //char 0 int c2i(char ch) { if(isdigit(ch)) return ch - 48; if( ch 'f' && ch 'z' ) return -1; if(isalpha(ch)) return isupper(ch) ? ch - 55 : ch - 87; return -1; } int hex2dec(char *hex) { int len; int num = 0; int temp; int bits; int i; char str[64] = {0}; if(null==hex) { printf(input para error ); return 0; } if(('0'==hex[0])&&(('x'==hex[1])||('x'==hex[1]))) { strcpy(str,&hex[2]); }else { strcpy(str,hex); } printf(input num = %s ,str); len = strlen(str); for (i=0, temp=0; i运行结果:
biao@ubuntu:~/test/flash$ gcc createfile.cpp biao@ubuntu:~/test/flash$ lsa.out createfile.cpp hex2dec.cpp main.cpp out.binbiao@ubuntu:~/test/flash$ ./a.out ./out.bin 0x10input num = 10 need to write data len 16 fill data vale = 0x30 biao@ubuntu:~/test/flash$ lsa.out createfile.cpp hex2dec.cpp main.cpp out.binbiao@ubuntu:~/test/flash$ vim out.bin 1 0000000000000000
4、批量处理图片
功能:批处理将图片前面固定的字节数删除。
/*=============================================================================# filename: cutfile.cpp# desc: 批量处理,将图片的前面固定字节删除# author: caibiao lee# version: # lastchange: 2018-11-26 # history:=============================================================================*/#include #include #include #include #include #define start_read_position 128#define photo_start_time 83641//l_s32phototime = 92809; int cut_file(char * inputfile){ file *l_pfileinput = null; file *l_pfileoutput = null; char l_ars8outputname[128] = {0}; unsigned char l_arru8tempdata[1024] = {0}; int l_s32ret = 0; static unsigned int ls_u32num = 0; if(null== inputfile) { goto error; } //sprintf(l_ars8outputname,./outfile/_%s,&inputfile[8]); sprintf(l_ars8outputname,./outfile/00%d.jpg,ls_u32num++); //printf(out file name %s ,l_ars8outputname); l_pfileinput = fopen(inputfile,rb+); if(null==l_pfileinput) { printf(input file open error); goto error; } l_pfileoutput = fopen(l_ars8outputname,w+); if(null==l_pfileoutput) { printf(out file open error); goto error; } fseek(l_pfileinput,start_read_position,seek_set); while(!feof(l_pfileinput)) { l_s32ret = fread(l_arru8tempdata,1,1024,l_pfileinput); if(l_s32ret<0) { break; } l_s32ret = fwrite(l_arru8tempdata,1,l_s32ret,l_pfileoutput); if(l_s32ret<0) { break; } } error: if(null!=l_pfileoutput) { fclose(l_pfileoutput); l_pfileoutput =null; }; if(null !=l_pfileinput); { fclose(l_pfileinput); l_pfileinput =null; }} int main(void){ char l_arrs8inputname[128] = {0}; char l_s8photochannel = 0; int l_s32phototime = 0; l_s8photochannel = 3; l_s32phototime = photo_start_time; /**从第一通道开始**/ for(int j=1;j运行结果:
biao@ubuntu:~/test/photo$ gcc cutfile.cpp biao@ubuntu:~/test/photo$ lsa.out cutfile.cpp image outfilebiao@ubuntu:~/test/photo$ ./a.out ./image/1y083642.jpg./image/1y083714.jpg./image/1y083747.jpg./image/1y083820.jpg./image/1y083853.jpg./image/1y083925.jpg./image/1y084157.jpg./image/1y084228.jpg./image/1y084301.jpg./image/1y084334.jpg./image/1y084406.jpg./image/1y084439.jpg./image/1y084711.jpg./image/1y084742.jpg./image/1y173524.jpg./image/1y173556.jpg./image/1y173629.jpg./image/1y173702.jpg./image/1y173933.jpg./image/1y174004.jpg./image/1y174244.jpg./image/1y174315.jpg./image/1y174348.jpg./image/1y174420.jpg./image/1y174454.jpg./image/1y174733.jpgbiao@ubuntu:~/test/photo$ tree.├── a.out├── cutfile.cpp├── image│ ├── 1y083642.jpg│ ├── 1y083714.jpg│ ├── 1y083747.jpg│ ├── 1y083820.jpg│ ├── 1y083853.jpg│ ├── 1y083925.jpg│ ├── 1y084157.jpg│ ├── 1y084228.jpg│ ├── 1y084301.jpg│ ├── 1y084334.jpg│ ├── 1y084406.jpg│ ├── 1y084439.jpg│ ├── 1y084711.jpg│ ├── 1y084742.jpg│ ├── 1y173524.jpg│ ├── 1y173556.jpg│ ├── 1y173629.jpg│ ├── 1y173702.jpg│ ├── 1y173933.jpg│ ├── 1y174004.jpg│ ├── 1y174244.jpg│ ├── 1y174315.jpg│ ├── 1y174348.jpg│ ├── 1y174420.jpg│ ├── 1y174454.jpg│ └── 1y174733.jpg└── outfile ├── 000.jpg ├── 0010.jpg ├── 0011.jpg ├── 0012.jpg ├── 0013.jpg ├── 0014.jpg ├── 0015.jpg ├── 0016.jpg ├── 0017.jpg ├── 0018.jpg ├── 0019.jpg ├── 001.jpg ├── 0020.jpg ├── 0021.jpg ├── 0022.jpg ├── 0023.jpg ├── 0024.jpg ├── 0025.jpg ├── 002.jpg ├── 003.jpg ├── 004.jpg ├── 005.jpg ├── 006.jpg ├── 007.jpg ├── 008.jpg └── 009.jpg 2 directories, 54 filesbiao@ubuntu:~/test/photo$
运行前需要创建两个目录,image用来存放需要处理的图片,outfile用来存放处理过后的文件。这种处理文件批处理方式很暴力,偶尔用用还是可以的。
5、io控制小程序
嵌入式设备系统一般为了节省空间,一般都会对系统进行裁剪,所以很多有用的命令都会被删除。在嵌入式设备中要调试代码也是比较麻烦的,一般只能看串口打印。现在写了个小程序,专门用来查看和控制海思hi3520dv300芯片的io电平状态。
/*=============================================================================# filename: hi3520_io_ctrl.cpp# desc: hi3520dv300 io write and read# author: caibiao lee# version: # lastchange: 2018-11-30# history:=============================================================================*/#include #include #include hstgpioal.h int printfinputtips(char *ps8name){ printf(=========== error!!! ========); printf(usage write: %s gpio bit value , ps8name); printf(usage read : %s gpio bit , ps8name); printf(eg write 1 to gpio1_bit02 : %s 1 2 1, ps8name); printf(eg read gpio1_bit02 value : %s 1 2 , ps8name); printf(=============bt20==================) printf(usb hub gpio_0_2 1_up; 0_down ); printf(reset_hd gpio_13_0 0_en; 1_disen); printf(power_hd gpio_13_3 1_up; 0_down ); return 0;} int main(int argc, char **argv){ if((3!=argc)&&(4!=argc)) { printfinputtips(argv[0]); return -1; } unsigned char l_u8gpionum = 0; unsigned char l_u8gpiobit = 0; unsigned char l_u8setvalue = 0; gpio_group_e l_egpiogroup; gpio_bit_e l_ebit; gpio_data_e l_edata; l_u8gpionum = atoi(argv[1]); l_u8gpiobit = atoi(argv[2]); if(l_u8gpionum<14) { l_egpiogroup = (gpio_group_e)l_u8gpionum; }else { printf(l_u8gpionum error l_u8gpionum = %d,l_u8gpionum); return -1; }; if(l_u8gpiobit 0) { l_s32ret = fwrite(l_arru8temp,1,l_s32ret,pfbasic); if(l_s32ret insertdata(l_pfbasec,l_pfuboot,uboot_position)) { printf(line %d error ,__line__); goto error; } if(0> insertdata(l_pfbasec,l_pfkernel,kernel_position)) { printf(line %d error ,__line__); goto error; } if(0> insertdata(l_pfbasec,l_pfrootfs,rootfs_position)) { printf(line %d error ,__line__); goto error; } if(0> insertdata(l_pfbasec,l_pfapp,app_position)) { printf(line %d error ,__line__); goto error; } error: if(null!=l_pfbasec) { fclose(l_pfbasec); l_pfbasec = null; } if(null!=l_pfuboot) { fclose(l_pfuboot); l_pfuboot = null; } if(null!=l_pfkernel) { fclose(l_pfkernel); l_pfkernel = null; } if(null!=l_pfrootfs) { fclose(l_pfrootfs); l_pfrootfs = null; } if(null!=l_pfapp) { fclose(l_pfapp); l_pfapp = null; } return 0;}
7、获取本地ip地址
在linux设备中获取本地ip地址可以使用下面的程序,支持最大主机有三个网口的设备,当然这个网卡数可以修改。
京东组团打造推广联盟 OLED电视爆发在即
中国光伏制造商本年二季度财报预期多元化
科技公司ANRA Technologies发表了新一代的DroneOSS平台
苹果新专利: AirPods可代替智能手环
氢燃料领域已经研究多年 如今成效十分显著
几个实用的嵌入式C程序分享
浅析串口屏通讯协议
Pandora推出互动语音广告以选择用户
频谱分析仪重要性能指标解析
非技术专家如何利用机器学习解决问题?
小米推出8周年纪念版手机
真的来了!官方曝定制版小米手环3腕带上将镭雕“ARE YOU OK?”
借助AI在网络边缘训练设备的价值
INA116基本连接电路
XP Power推出新款500W-650W AC-DC电源,满足医疗设备(包括BF)应用
如何让小型云台机械手实现按颜色分拣物品?
华为5G专利申请量全球第一
2022~2028年LED照明行业市场研究及发展趋势
PCB:PCB领域的加税主要针对电视机的PCB及生产设备
iphone8被曝光提前6月量产 三星S8渲染图曝光