在學習和工作開發(fā)的時候,經(jīng)常需要使用到各種各樣不太常用的操作,這種情況一般是自己手動寫一些小程序來處理。因為它們不太常用,所以經(jīng)常用了又沒保存,等到下一次在使用的時候又需要重寫,這樣的非常浪費時間和精力。
所以想在這里統(tǒng)一記錄一下,以備下次重新使用。代碼以實用為主,如果缺陷,歡迎指出。
1 十六進制字符轉(zhuǎn)整型數(shù)字
功能:
將16進制的字符串轉(zhuǎn)換為10進制的數(shù)字。我是沒有找到相應的庫函數(shù),所以參考網(wǎng)上的代碼自己手動寫了個函數(shù)來實現(xiàn)。
常用的函數(shù)有atoi,atol,他們都是將10進制的數(shù)字字符串轉(zhuǎn)換為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) { // 如果是數(shù)字,則用數(shù)字的ASCII碼減去48, 如果ch = '2' ,則 '2' - 48 = 2 if(isdigit(ch)) return ch - 48; // 如果是字母,但不是A~F,a~f則返回 if( ch < 'A' || (ch > 'F' && ch < 'a') || ch > 'z' ) return -1; // 如果是大寫字母,則用數(shù)字的ASCII碼減去55, 如果ch = 'A' ,則 'A' - 55 = 10 // 如果是小寫字母,則用數(shù)字的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函數(shù)傳遞的 len = strlen(str); for (i=0, temp=0; i
2 字符串轉(zhuǎn)整型
功能:
將正常輸入的16進制或是10進制的字符串轉(zhuǎn)換為int數(shù)據(jù)類型。
/*============================================================================= # FileName: hex2dec.cpp # Desc: Convert a hex/dec string to a int number # Author: Caibiao Lee # Version: # LastChange: 2018-12-03 # History: =============================================================================*/ #include#include #include #include int String2int(char *strChar) { int len=0; const char *pstrCmp1="0123456789ABCDEF"; const char *pstrCmp2="0123456789abcdef"; char *pstr=NULL; int uiValue=0; int j=0; unsigned int t=0; int i=0; if(NULL==strChar) return -1; if(0>=(len=strlen((const char *)strChar))) return -1; if(NULL!=(pstr=strstr(strChar,"0x"))||NULL!=(pstr=strstr(strChar,"0X"))) { pstr=(char *)strChar+2; if(0>=(len=strlen((const char *)pstr))) return -1; for(i=(len-1);i>=0;i--) { if(pstr[i]>'F') { for(t=0;t
3 創(chuàng)建文件并填充固定數(shù)據(jù)
功能:
創(chuàng)建固定大小的一個文件,并且把這個文件填充為固定的數(shù)據(jù)。
/*============================================================================= # FileName: CreateFile.cpp # Desc: 創(chuàng)建固定大小的文件,然后填充固定的數(shù)據(jù) # 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 < 'A' || (ch > 'F' && ch < 'a') || 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
運行結(jié)果:
biao@ubuntu:~/test/flash$ gcc CreateFile.cpp biao@ubuntu:~/test/flash$ ls a.out CreateFile.cpp hex2dec.cpp main.cpp out.bin biao@ubuntu:~/test/flash$ ./a.out ./out.bin 0x10 input num = 10 Need To Write Data Len 16 Fill Data Vale = 0x30 biao@ubuntu:~/test/flash$ ls a.out CreateFile.cpp hex2dec.cpp main.cpp out.bin biao@ubuntu:~/test/flash$ vim out.bin 1 0000000000000000
4 批量處理圖片
功能:
批處理將圖片前面固定的字節(jié)數(shù)刪除。
/*============================================================================= # FileName: CutFile.cpp # Desc: 批量處理,將圖片的前面固定字節(jié)刪除 # 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
運行結(jié)果:
biao@ubuntu:~/test/photo$ gcc CutFile.cpp biao@ubuntu:~/test/photo$ ls a.out CutFile.cpp image outfile biao@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.jpg biao@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 files biao@ubuntu:~/test/photo$運行前需要創(chuàng)建兩個目錄,image用來存放需要處理的圖片,outfile用來存放處理過后的文件。這種處理文件批處理方式很暴力,偶爾用用還是可以的。
5 IO控制小程序
嵌入式設備系統(tǒng)一般為了節(jié)省空間,一般都會對系統(tǒng)進行裁剪,所以很多有用的命令都會被刪除。在嵌入式設備中要調(diào)試代碼也是比較麻煩的,一般只能看串口打印。現(xiàn)在寫了個小程序,專門用來查看和控制海思Hi3520DV300芯片的IO電平狀態(tài)。
/*============================================================================= # 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<8) { l_eBit = (GPIO_BIT_E)l_u8GPIOBit; }else { printf("l_u8GPIOBit error l_u8GPIOBit = %d ",l_u8GPIOBit); return -1; } if(NULL!=argv[3]) { l_u8SetValue = atoi(argv[3]); if(0==l_u8SetValue) { l_eData = (GPIO_DATA_E)l_u8SetValue; }else if(1==l_u8SetValue) { l_eData = (GPIO_DATA_E)l_u8SetValue; }else { printf("l_u8SetValue error l_u8SetValue = %d ",l_u8SetValue); } } if(3==argc) {/**read**/ printf("read GPIO%d Bit%d ",l_u8GPIONum,l_u8GPIOBit); /**set input**/ HstGpio_Set_Direction(l_eGpioGroup, l_eBit, GPIO_INPUT); /**read **/ char l_s8bit_val = 0; HstGpio_Get_Value(l_eGpioGroup, l_eBit, &l_s8bit_val); printf("read Data = %d ",l_s8bit_val); }else if(4==argc) {/**write**/ printf("Write GPIO %d; Bit %d; Value %d ",l_u8GPIONum,l_u8GPIOBit,l_u8SetValue); /***set IO output*/ HstGpio_Set_Direction(l_eGpioGroup, l_eBit, GPIO_OUPUT); /**Write To IO**/ HstGpio_Set_Value(l_eGpioGroup,l_eBit,l_eData); }else { } return 0; }
6 文件固定位置插入數(shù)據(jù)
在文件的固定位置插入固定的數(shù)據(jù)。
#include#include #include #define BASIC_FILE_NAME "./nandflash.bin" #define UBOOT_FILE_NAME "./u-boot.bin" #define KERNEL_FILE_NAME "./kernel.bin" #define ROOTFS_FILE_NAME "./rootfs.bin" #define APP_FILE_NAME "./app.bin" #define UBOOT_POSITION 0x00 #define KERNEL_POSITION 0x100000 #define ROOTFS_POSITION 0x500000 #define APP_POSITION 0x2700000 int InsertData(FILE *pfBasic,FILE *psInsert,int s32Position) { int l_S32Ret = 0; unsigned char l_arru8Temp[1024] = {0xff}; fseek(pfBasic,s32Position,SEEK_SET); fseek(psInsert,0,SEEK_SET); while(1) { l_S32Ret = fread(l_arru8Temp,1,1024,psInsert); if(l_S32Ret > 0) { l_S32Ret = fwrite(l_arru8Temp,1,l_S32Ret,pfBasic); if(l_S32Ret<=0) { printf("line %d error l_S32Ret = %d ",__LINE__,l_S32Ret); return -1; } }else { break; } } return 0; } int main(void) { int l_s32Ret = 0; FILE *l_pfBasec = NULL; FILE *l_pfUboot = NULL; FILE *l_pfKernel = NULL; FILE *l_pfRootfs = NULL; FILE *l_pfApp = NULL; l_pfBasec = fopen(BASIC_FILE_NAME,"r+"); if(NULL==l_pfBasec) { printf("line %d error ",__LINE__); goto ERROR; } l_pfUboot = fopen(UBOOT_FILE_NAME,"r"); if(NULL==l_pfUboot) { printf("line %d error ",__LINE__); goto ERROR; } l_pfKernel = fopen(KERNEL_FILE_NAME,"r"); if(NULL==l_pfKernel) { printf("line %d error ",__LINE__); goto ERROR; } l_pfRootfs = fopen(ROOTFS_FILE_NAME,"r"); if(NULL==l_pfRootfs) { printf("line %d error ",__LINE__); goto ERROR; } l_pfApp = fopen(APP_FILE_NAME,"r"); if(NULL==l_pfApp) { printf("line %d error ",__LINE__); goto ERROR; } if(0> 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地址可以使用下面的程序,支持最大主機有三個網(wǎng)口的設備,當然這個網(wǎng)卡數(shù)可以修改。
#include來源:https://caibiao-lee.blog.csdn.net/?type=blog#include #include #include #include int get_local_ip(char *ps8IpList) { struct ifaddrs *ifAddrStruct; char l_s8IpAddr[INET_ADDRSTRLEN]; void *tmpAddrPtr; int l_s32IPCount = 0; getifaddrs(&ifAddrStruct); while (ifAddrStruct != NULL) { if (ifAddrStruct->ifa_addr->sa_family==AF_INET) { tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr; inet_ntop(AF_INET, tmpAddrPtr, l_s8IpAddr, INET_ADDRSTRLEN); if (strcmp(l_s8IpAddr, "127.0.0.1") != 0) { if(l_s32IPCount == 0) { memcpy(ps8IpList, l_s8IpAddr, INET_ADDRSTRLEN); } else { memcpy(ps8IpList+INET_ADDRSTRLEN, l_s8IpAddr, INET_ADDRSTRLEN); } l_s32IPCount++; } } ifAddrStruct=ifAddrStruct->ifa_next; } freeifaddrs(ifAddrStruct); return l_s32IPCount; } int main() { char l_arrs8IpAddrList[3][INET_ADDRSTRLEN]; int l_s32AddrCount; memset(l_arrs8IpAddrList, 0, sizeof(l_arrs8IpAddrList)); l_s32AddrCount = get_local_ip(*l_arrs8IpAddrList); for(l_s32AddrCount;l_s32AddrCount>0;l_s32AddrCount--) { printf("Server Local IP%d: %s ",l_s32AddrCount,l_arrs8IpAddrList[l_s32AddrCount-1]); } return 0; }
審核編輯:劉清
-
轉(zhuǎn)換器
+關注
關注
27文章
8607瀏覽量
146753 -
嵌入式系統(tǒng)
+關注
關注
41文章
3554瀏覽量
129142 -
ASCII碼
+關注
關注
0文章
51瀏覽量
17288
原文標題:幾個實用的嵌入式C程序代碼塊
文章出處:【微信號:c-stm32,微信公眾號:STM32嵌入式開發(fā)】歡迎添加關注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關推薦
評論