在学习和工作开发的时候,经常需要使用到各种各样不太常用的操作,这种情况一般是自己手动写一些小程序来处理。因为它们不太常用,所以经常用了又没保存,等到下一次在使用的时候又需要重写,这样的非常浪费时间和精力。 所以想在这里统一记录一下,以备下次重新使用。代码以实用为主,如果缺陷,欢迎指出。 1、十六进制字符转整型数字 功能:将16进制的字符串转换为10进制的数字。我是没有找到相应的库函数,所以参考网上的代码自己手动写了个函数来实现。 常用的函数有atoi,atol,他们都是将10进制的数字字符串转换为int或是long类型,所以在有些情况下不适用。FileName:hex2dec。cppDesc:ConvertahexstringtoaintnumberAuthor:CaibiaoLeeVersion:LastChange:20181126History:includestdio。hincludestdlib。hincludestring。hincludectype。hintc2i(charch){如果是数字,则用数字的ASCII码减去48,如果ch2,则2482if(isdigit(ch))returnch48;如果是字母,但不是AF,af则返回if(chA(chFcha)chz)return1;如果是大写字母,则用数字的ASCII码减去55,如果chA,则A5510如果是小写字母,则用数字的ASCII码减去87,如果cha,则a8710if(isalpha(ch))returnisupper(ch)?ch55:ch87;return1;}inthex2dec(charhex){intlen;intnum0;inttemp;intbits;inti;charstr〔64〕{0};if(NULLhex){printf(inputparaerror);return0;}if((0hex〔0〕)((Xhex〔1〕)(xhex〔1〕))){strcpy(str,hex〔2〕);}else{strcpy(str,hex);}printf(inputnums,str);此例中str1de长度为3,hex是main函数传递的lenstrlen(str);for(i0,temp0;ilen;i,temp0){第一次:i0,(stri)(str0)1,即temp1第二次:i1,(stri)(str1)d,即temp13第三次:i2,(stri)(str2)d,即temp14tempc2i((stri));总共3位,一个16进制位用4bit保存第一次:1为最高位,所以temp左移(leni1)4248位第二次:d为次高位,所以temp左移(leni1)4144位第三次:e为最低位,所以temp左移(leni1)4040位bits(leni1)4;temptempbits;此处也可以用numtemp;进行累加numnumtemp;}返回结果returnnum;}intmain(intargc,charargv){intls32Ret0;if(2!argc){printf(ERROR!);printf(usage:sNum,argv〔0〕);printf(eg1:s0x400,argv〔0〕);return0;}ls32Rethex2dec(argv〔1〕);printf(valuehex0xx,ls32Ret);printf(valuedecd,ls32Ret);return0;} 运行结果:biaoubuntu:testflashnbsp;。a。out0x400inputnum400valuehex0x400valuedec1024biaoubuntu:testflashnbsp; 2、字符串转整型 功能:将正常输入的16进制或是10进制的字符串转换为int数据类型。FileName:hex2dec。cppDesc:ConvertahexdecstringtoaintnumberAuthor:CaibiaoLeeVersion:LastChange:20181203History:includestdio。hincludestdlib。hincludestring。hincludectype。hintString2int(charstrChar){intlen0;constcharpstrCmp10123456789ABCDEF;constcharpstrCmp20123456789abcdef;charpstrNULL;intuiValue0;intj0;unsignedintt0;inti0;if(NULLstrChar)return1;if(0(lenstrlen((constchar)strChar)))return1;if(NULL!(pstrstrstr(strChar,0x))NULL!(pstrstrstr(strChar,0X))){pstr(char)strChar2;if(0(lenstrlen((constchar)pstr)))return1;for(i(len1);i0;i){if(pstr〔i〕F){for(t0;tstrlen((constchar)pstrCmp2);t){if(pstrCmp2〔t〕pstr〔i〕)uiValue(t(j4));}}else{for(t0;tstrlen((constchar)pstrCmp1);t){if(pstrCmp1〔t〕pstr〔i〕)uiValue(t(j4));}}}}else{uiValueatoi((constchar)strChar);}returnuiValue;}intmain(intargc,charargv){intls32Ret0;if(2!argc){printf(ERROR!);printf(usage:sNum,argv〔0〕);printf(eg1:s0x400,argv〔0〕);return0;}ls32RetString2int(argv〔1〕);printf(valuehex0xx,ls32Ret);printf(valuedecd,ls32Ret);return0;} 3、创建文件并填充固定数据 功能:创建固定大小的一个文件,并且把这个文件填充为固定的数据。FileName:CreateFile。cppDesc:创建固定大小的文件,然后填充固定的数据Author:CaibiaoLeeVersion:LastChange:20181126History:includestdio。hincludestdlib。hincludestring。hincludectype。hdefineFILLDATAVALUE0xffdefineFILLDATAVALUE0x30char0intc2i(charch){if(isdigit(ch))returnch48;if(chA(chFcha)chz)return1;if(isalpha(ch))returnisupper(ch)?ch55:ch87;return1;}inthex2dec(charhex){intlen;intnum0;inttemp;intbits;inti;charstr〔64〕{0};if(NULLhex){printf(inputparaerror);return0;}if((0hex〔0〕)((Xhex〔1〕)(xhex〔1〕))){strcpy(str,hex〔2〕);}else{strcpy(str,hex);}printf(inputnums,str);lenstrlen(str);for(i0,temp0;ilen;i,temp0){tempc2i((stri));bits(leni1)4;temptempbits;numnumtemp;}returnnum;}intmain(intargc,charargv){FILElpFileNULL;intls32Rest0;unsignedintlWriteLen0;unsignedintlFileLen0;unsignedcharTempData〔1024〕{FILLDATAVALUE};if(3!argc){printf(usage:sFileNameFileLen,argv〔0〕);printf(eg:s。Outfile。bin0x400,argv〔0〕);return0;};constcharlpFileNameargv〔1〕;if(NULLlpFileName){printf(inputfilenameisNULL);return1;}if((0argv〔2〕〔0〕)((Xargv〔2〕〔1〕)(xargv〔2〕〔1〕))){lFileLenhex2dec(argv〔2〕);}else{lFileLenatoi(argv〔2〕);}printf(NeedToWriteDataLend,lFileLen);printf(FillDataVale0xx,FILLDATAVALUE);for(inti0;i1024;i){TempData〔i〕FILLDATAVALUE;}lpFilefopen(lpFileName,w);if(lpFileNULL){printf(openfileserror,lpFileName);return1;}while(lWriteLenlFileLen){if(lFileLen1024){ls32Restfwrite(TempData,1,lFileLen,lpFile);}else{ls32Restfwrite(TempData,1,1024,lpFile);}if(ls32Rest0){break;};lWriteLenls32Rest;}if(NULL!lpFile){fclose(lpFile);lpFileNULL;}return0;} 运行结果:biaoubuntu:testflashnbsp;gccCreateFile。cppbiaoubuntu:testflashnbsp;lsa。outCreateFile。cpphex2dec。cppmain。cppout。binbiaoubuntu:testflashnbsp;。a。out。out。bin0x10inputnum10NeedToWriteDataLen16FillDataVale0x30biaoubuntu:testflashnbsp;lsa。outCreateFile。cpphex2dec。cppmain。cppout。binbiaoubuntu:testflashnbsp;vimout。bin10000000000000000 4、批量处理图片 功能:批处理将图片前面固定的字节数删除。FileName:CutFile。cppDesc:批量处理,将图片的前面固定字节删除Author:CaibiaoLeeVersion:LastChange:20181126History:includestdio。hincludestdlib。hincludestring。hincludeunistd。hincludesysstat。hdefineSTARTREADPOSITION128definePHOTOSTARTTIME83641ls32PhotoTime92809;intCutfile(charInputFile){FILElpFileInputNULL;FILElpFileOutputNULL;charlars8OutputName〔128〕{0};unsignedcharlarru8TempData〔1024〕{0};intls32Ret0;staticunsignedintlsu32Num0;if(NULLInputFile){gotoERROR;}sprintf(lars8OutputName,。outfiles,InputFile〔8〕);sprintf(lars8OutputName,。outfile00d。jpg,lsu32Num);printf(outfilenames,lars8OutputName);lpFileInputfopen(InputFile,rb);if(NULLlpFileInput){printf(inputfileopenerror);gotoERROR;}lpFileOutputfopen(lars8OutputName,w);if(NULLlpFileOutput){printf(outfileopenerror);gotoERROR;}fseek(lpFileInput,STARTREADPOSITION,SEEKSET);while(!feof(lpFileInput)){ls32Retfread(larru8TempData,1,1024,lpFileInput);if(ls32Ret0){break;}ls32Retfwrite(larru8TempData,1,ls32Ret,lpFileOutput);if(ls32Ret0){break;}}ERROR:if(NULL!lpFileOutput){fclose(lpFileOutput);lpFileOutputNULL;};if(NULL!lpFileInput);{fclose(lpFileInput);lpFileInputNULL;}}intmain(void){charlarrs8InputName〔128〕{0};charls8PhotoChannel0;intls32PhotoTime0;ls8PhotoChannel3;ls32PhotoTimePHOTOSTARTTIME;从第一通道开始for(intj1;jls8PhotoChannel;j){for(intils32PhotoTime;i235959;i){memset(larrs8InputName,0,sizeof(larrs8InputName));sprintf(larrs8InputName,。imagedY06d。jpg,j,i);if(0access(larrs8InputName,FOK)){printf(s,larrs8InputName);Cutfile(larrs8InputName);}}}} 运行结果:biaoubuntu:testphotonbsp;gccCutFile。cppbiaoubuntu:testphotonbsp;lsa。outCutFile。cppimageoutfilebiaoubuntu:testphotonbsp;。a。out。image1Y083642。jpg。image1Y083714。jpg。image1Y083747。jpg。image1Y083820。jpg。image1Y083853。jpg。image1Y083925。jpg。image1Y084157。jpg。image1Y084228。jpg。image1Y084301。jpg。image1Y084334。jpg。image1Y084406。jpg。image1Y084439。jpg。image1Y084711。jpg。image1Y084742。jpg。image1Y173524。jpg。image1Y173556。jpg。image1Y173629。jpg。image1Y173702。jpg。image1Y173933。jpg。image1Y174004。jpg。image1Y174244。jpg。image1Y174315。jpg。image1Y174348。jpg。image1Y174420。jpg。image1Y174454。jpg。image1Y174733。jpgbiaoubuntu:testphotonbsp;tree。a。outCutFile。cppimage1Y083642。jpg1Y083714。jpg1Y083747。jpg1Y083820。jpg1Y083853。jpg1Y083925。jpg1Y084157。jpg1Y084228。jpg1Y084301。jpg1Y084334。jpg1Y084406。jpg1Y084439。jpg1Y084711。jpg1Y084742。jpg1Y173524。jpg1Y173556。jpg1Y173629。jpg1Y173702。jpg1Y173933。jpg1Y174004。jpg1Y174244。jpg1Y174315。jpg1Y174348。jpg1Y174420。jpg1Y174454。jpg1Y174733。jpgoutfile000。jpg0010。jpg0011。jpg0012。jpg0013。jpg0014。jpg0015。jpg0016。jpg0017。jpg0018。jpg0019。jpg001。jpg0020。jpg0021。jpg0022。jpg0023。jpg0024。jpg0025。jpg002。jpg003。jpg004。jpg005。jpg006。jpg007。jpg008。jpg009。jpg2directories,54filesbiaoubuntu:testphotonbsp; 运行前需要创建两个目录,image用来存放需要处理的图片,outfile用来存放处理过后的文件。这种处理文件批处理方式很暴力,偶尔用用还是可以的。 5、IO控制小程序 嵌入式设备系统一般为了节省空间,一般都会对系统进行裁剪,所以很多有用的命令都会被删除。 在嵌入式设备中要调试代码也是比较麻烦的,一般只能看串口打印。现在写了个小程序,专门用来查看和控制海思Hi3520DV300芯片的IO电平状态。FileName:Hi3520IOCTRL。cppDesc:Hi3520DV300IOWriteandReadAuthor:CaibiaoLeeVersion:LastChange:20181130History:includestdio。hincludestdlib。hincludehstGpioAL。hintPrintfInputTips(charps8Name){printf(error!!!);printf(usageWrite:sGPIObitvalue,ps8Name);printf(usageRead:sGPIObit,ps8Name);printf(egWrite1toGPIO1bit02:s121,ps8Name);printf(egReadGPIO1bit02Value:s12,ps8Name);printf(BT20)printf(USBHUBGPIO021UP;0Down);printf(RESETHDGPIO1300EN;1disEN);printf(PowerHDGPIO1331UP;0Down);return0;}intmain(intargc,charargv){if((3!argc)(4!argc)){PrintfInputTips(argv〔0〕);return1;}unsignedcharlu8GPIONum0;unsignedcharlu8GPIOBit0;unsignedcharlu8SetValue0;GPIOGROUPEleGpioGroup;GPIOBITEleBit;GPIODATAEleData;lu8GPIONumatoi(argv〔1〕);lu8GPIOBitatoi(argv〔2〕);if(lu8GPIONum14){leGpioGroup(GPIOGROUPE)lu8GPIONum;}else{printf(lu8GPIONumerrorlu8GPIONumd,lu8GPIONum);return1;};if(lu8GPIOBit8){leBit(GPIOBITE)lu8GPIOBit;}else{printf(lu8GPIOBiterrorlu8GPIOBitd,lu8GPIOBit);return1;}if(NULL!argv〔3〕){lu8SetValueatoi(argv〔3〕);if(0lu8SetValue){leData(GPIODATAE)lu8SetValue;}elseif(1lu8SetValue){leData(GPIODATAE)lu8SetValue;}else{printf(lu8SetValueerrorlu8SetValued,lu8SetValue);}}if(3argc){readprintf(readGPIOdBitd,lu8GPIONum,lu8GPIOBit);setinputHstGpioSetDirection(leGpioGroup,leBit,GPIOINPUT);readcharls8bitval0;HstGpioGetValue(leGpioGroup,leBit,ls8bitval);printf(readDatad,ls8bitval);}elseif(4argc){writeprintf(WriteGPIOd;Bitd;Valued,lu8GPIONum,lu8GPIOBit,lu8SetValue);setIOoutputHstGpioSetDirection(leGpioGroup,leBit,GPIOOUPUT);WriteToIOHstGpioSetValue(leGpioGroup,leBit,leData);}else{}return0;} 6、文件固定位置插入数据 在文件的固定位置插入固定的数据。includestdio。hincludestdlib。hincludestring。hdefineBASICFILENAME。nandflash。bindefineUBOOTFILENAME。uboot。bindefineKERNELFILENAME。kernel。bindefineROOTFSFILENAME。rootfs。bindefineAPPFILENAME。app。bindefineUBOOTPOSITION0x00defineKERNELPOSITION0x100000defineROOTFSPOSITION0x500000defineAPPPOSITION0x2700000intInsertData(FILEpfBasic,FILEpsInsert,ints32Position){intlS32Ret0;unsignedcharlarru8Temp〔1024〕{0xff};fseek(pfBasic,s32Position,SEEKSET);fseek(psInsert,0,SEEKSET);while(1){lS32Retfread(larru8Temp,1,1024,psInsert);if(lS32Ret0){lS32Retfwrite(larru8Temp,1,lS32Ret,pfBasic);if(lS32Ret0){printf(linederrorlS32Retd,LINE,lS32Ret);return1;}}else{break;}}return0;}intmain(void){intls32Ret0;FILElpfBasecNULL;FILElpfUbootNULL;FILElpfKernelNULL;FILElpfRootfsNULL;FILElpfAppNULL;lpfBasecfopen(BASICFILENAME,r);if(NULLlpfBasec){printf(linederror,LINE);gotoERROR;}lpfUbootfopen(UBOOTFILENAME,r);if(NULLlpfUboot){printf(linederror,LINE);gotoERROR;}lpfKernelfopen(KERNELFILENAME,r);if(NULLlpfKernel){printf(linederror,LINE);gotoERROR;}lpfRootfsfopen(ROOTFSFILENAME,r);if(NULLlpfRootfs){printf(linederror,LINE);gotoERROR;}lpfAppfopen(APPFILENAME,r);if(NULLlpfApp){printf(linederror,LINE);gotoERROR;}if(0InsertData(lpfBasec,lpfUboot,UBOOTPOSITION)){printf(linederror,LINE);gotoERROR;}if(0InsertData(lpfBasec,lpfKernel,KERNELPOSITION)){printf(linederror,LINE);gotoERROR;}if(0InsertData(lpfBasec,lpfRootfs,ROOTFSPOSITION)){printf(linederror,LINE);gotoERROR;}if(0InsertData(lpfBasec,lpfApp,APPPOSITION)){printf(linederror,LINE);gotoERROR;}ERROR:if(NULL!lpfBasec){fclose(lpfBasec);lpfBasecNULL;}if(NULL!lpfUboot){fclose(lpfUboot);lpfUbootNULL;}if(NULL!lpfKernel){fclose(lpfKernel);lpfKernelNULL;}if(NULL!lpfRootfs){fclose(lpfRootfs);lpfRootfsNULL;}if(NULL!lpfApp){fclose(lpfApp);lpfAppNULL;}return0;} 7、获取本地IP地址 在linux设备中获取本地IP地址可以使用下面的程序,支持最大主机有三个网口的设备,当然这个网卡数可以修改。includestdio。hincludeifaddrs。hincludenetinetin。hincludestring。hincludeintgetlocalip(charps8IpList){structifaddrsifAddrStruct;charls8IpAddr〔INETADDRSTRLEN〕;voidtmpAddrPtr;intls32IPCount0;getifaddrs(ifAddrStruct);while(ifAddrStruct!NULL){if(ifAddrStructifaaddrsafamilyAFINET){tmpAddrPtr((structsockaddrin)ifAddrStructifaaddr)sinaddr;inetntop(AFINET,tmpAddrPtr,ls8IpAddr,INETADDRSTRLEN);if(strcmp(ls8IpAddr,127。0。0。1)!0){if(ls32IPCount0){memcpy(ps8IpList,ls8IpAddr,INETADDRSTRLEN);}else{memcpy(ps8IpListINETADDRSTRLEN,ls8IpAddr,INETADDRSTRLEN);}ls32IPCount;}}ifAddrStructifAddrStructifanext;}freeifaddrs(ifAddrStruct);returnls32IPCount;}intmain(){charlarrs8IpAddrList〔3〕〔INETADDRSTRLEN〕;intls32AddrCount;memset(larrs8IpAddrList,0,sizeof(larrs8IpAddrList));ls32AddrCountgetlocalip(larrs8IpAddrList);for(ls32AddrCount;ls32AddrCount0;ls32AddrCount){printf(ServerLocalIPd:s,ls32AddrCount,larrs8IpAddrList〔ls32AddrCount1〕);}return0;}