运行结果:
biao@ubuntu:~/test/flash$ ./a.out 0x400input num = 400 value hex = 0x400 value dec = 1024 biao@ubuntu:~/test/flash$
2、字符串转整型
功能:将正常输入的16进制或是10进制的字符串转换为int数据类型 。
/*=============================================================================# FileName: hex2dec.cpp# Desc: Convert a hex/dec string to a int number# Author: Caibiao Lee# Version: # LastChange: 2018-12-03 # History:=============================================================================*/#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int String2int(char *strChar){ int len=0; const char *pstrCmp1="0123456789ABCDEF"; const char *pstrCmp2="0123456789abcdef"; char *pstr=NULL; int uiValue=https://www.isolves.com/it/cxkf/yy/C/2022-02-28/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
功能:创建固定大小的一个文件 , 并且把这个文件填充为固定的数据 。
/*=============================================================================# FileName: CreateFile.cpp# Desc: 创建固定大小的文件 , 然后填充固定的数据# Author: Caibiao Lee# Version: # LastChange: 2018-11-26 # History:=============================================================================*/#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> //#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 n"); return 0; } if(('0'==hex[0])&&(('X'==hex[1])||('x'==hex[1]))) { strcpy(str,&hex[2]); }else { strcpy(str,hex); } printf("input num = %s n",str); len = strlen(str); for (i=0, temp=0; i<len; i++, temp=0) { temp = c2i( *(str + i) ); bits = (len - i - 1) * 4; temp = temp << bits; num = num | temp; } return num; } int main(int argc, char **argv){ FILE *l_pFile = NULL; int l_s32Rest = 0; unsigned int l_WriteLen = 0; unsigned int l_FileLen = 0; unsigned char TempData[1024] = {FILL_DATA_VALUE}; if(3!=argc) { printf("usage: %s FileName FileLen n ", argv[0]); printf("eg: %s ./Outfile.bin 0x400 n ", argv[0]); return 0; }; const char *l_pFileName = argv[1]; if(NULL==l_pFileName) { printf("input file name is NULL n"); return -1; } if(('0'==argv[2][0])&&(('X'==argv[2][1])||('x'==argv[2][1]))) { l_FileLen = hex2dec(argv[2]); }else { l_FileLen = atoi(argv[2]); } printf("Need To Write Data Len %d n",l_FileLen); printf("Fill Data Vale = 0x%x n",FILL_DATA_VALUE); for(int i=0;i<1024;i++) { TempData[i] = FILL_DATA_VALUE; } l_pFile = fopen(l_pFileName,"w+"); if(l_pFile==NULL) { printf("open file %s error n",l_pFileName); return -1; } while(l_WriteLen<l_FileLen) { if(l_FileLen<1024) { l_s32Rest = fwrite(TempData,1,l_FileLen,l_pFile); } else { l_s32Rest = fwrite(TempData,1,1024,l_pFile); } if(l_s32Rest <= 0) { break; }; l_WriteLen +=l_s32Rest; } if(NULL!=l_pFile) { fclose(l_pFile); l_pFile = NULL; } return 0; }
推荐阅读
- 人工智能文档编写器:使用AI生成Javadocs等文档的插件扩展
- Tomcat源码篇-简单到超乎想象的启动流程
- 金俊眉属于小种红茶吗,金俊眉茶的功效与作用女人们
- 云南女儿红茶功效,女儿环茶的作用功效
- 红茶具有安神作用的茶,茶梗枕头的功效与作用点
- 验证码的前世今生
- 红茶放的时间,五红茶的功效与作用
- 高级红茶的攻效,茶芽的功效与作用
- 红茶哪里买的好,发烧吃午时茶的功效与作用
- 红茶含水分,青梅茶的功效及作用