---basictype.h---
#ifndef __BASICTYPE_H__ #define __BASICTYPE_H__ typedef unsigned char uchar; typedef signed char schar; typedef unsigned short ushort; typedef signed short sshort; typedef unsigned int uint; typedef signed int sint; typedef unsigned long ulong; typedef signed long slong; typedef unsigned char uint8; typedef signed char sint8; typedef unsigned short uint16; typedef signed short sint16; typedef unsigned int uint32; typedef signed int sint32; typedef unsigned long ulong32; typedef signed long slong32; typedef unsigned char uint8_t; typedef signed char sint8_t; typedef unsigned short uint16_t; typedef signed short sint16_t; typedef unsigned int uint32_t; typedef signed int sint32_t; typedef unsigned long ulong32_t; typedef signed long slong32_t; #endif
---char2hex.h---
#ifndef __CHAR2HEX_H__ #define __CHAR2HEX_H__ #include "basictype.h" #define IS_HEX_CHAR(x) ((((x)>='0')&&((x)<='9'))||(((x)>='a')&&((x)<='f'))||(((x)>='A')&&((x)<='F'))) #define CHAR2HEX(x) ((((x)>='0')&&((x)<='9'))?((x)-'0'):(((x)>='a')&&((x)<='f'))?((x)-'a'+10):((x)-'A'+10)) bool is_hex_char(char x); bool char2hex(char x, int* v); bool char2num(char x, int* v); int hex_char_str_to_byte_stream(const char* p, uchar* buf, uint n); uint get_uint_value_from_hex_char_str(const char* p); uint get_uint_value_from_char_str(const char* p); #endif
---char2hex.cpp---
#include "char2hex.h" #ifndef NULL #define NULL ((void*)0) #endif bool is_hex_char(char x) { if (x >= '0' && x <= '9') { return true; } if (x >= 'a' && x <= 'f') { return true; } if (x >= 'A' && x <= 'F') { return true; } return false; } bool char2hex(char x, int *v) { if (v == NULL) { return false; } if (x >= '0' && x <= '9') { *v = x - '0'; return true; } if (x >= 'a' && x <= 'f') { *v = x - 'a' + 10; return true; } if (x >= 'A' && x <= 'F') { *v = x - 'A' + 10; return true; } return false; } bool char2num(char x, int* v) { if (v == NULL) { return false; } if (x >= '0' && x <= '9') { *v = x - '0'; return true; } return false; } /* * p is a pointer point to string such like "12 34 56 78 9A BC DE F0" * buf is a array to store convert result * n is the buf size * the return value is real convert size * in this example the convert size may be equal to 8 while n >= 8, if n < 8, the convert size is n */ int hex_char_str_to_byte_stream(const char* p, uchar* buf, uint n) { int i = 0; uchar hex = 0; int tmp1; int tmp2; //check par if (p == NULL || buf == NULL || n == 0) { return 0; } while (1) { if (char2hex(*p, &tmp1)) { p++; if (char2hex(*p, &tmp2)) { p++; if (*p == ' ') { p++; } hex = (tmp1 << 4) | tmp2; buf[i++] = hex; if (i >= n) { return n; } if (*p == 0) { return i; } } else { return 0; } } else { return 0; } } } uint get_uint_value_from_hex_char_str(const char* p) { uint ret = 0; if (p == NULL) { return 0; } if (*p == '0' && (*(p+1)) == 'x') { p += 2; } else if (*p == '0' && (*(p+1)) == 'X') { p += 2; } int tmp; while (char2hex(*p, &tmp)) { ret = (ret << 4) + tmp; p++; } return ret; } uint get_uint_value_from_char_str(const char* p) { uint ret = 0; uint base = 10; if (p == NULL) { return 0; } if (*p == '0' && (*(p + 1)) == 'x') { p += 2; base = 16; } else if (*p == '0' && (*(p + 1)) == 'X') { p += 2; base = 16; } int tmp; if (base == 16) { while (char2hex(*p, &tmp)) { ret = ret * base + tmp; p++; } } else { while (char2num(*p, &tmp)) { ret = ret * base + tmp; p++; } } return ret; }
标签:typedef,工程,int,基础,unsigned,signed,char,&& From: https://www.cnblogs.com/-FlyingFish-/p/17841179.html