<pre name="code" class="cpp">// NOTE: in mind
//
// '0':48
// 'A':65
// 'a':97
//
#define CHAR2INT(_c, _d) \
do { \
if(_c >= '0' && _c <= '9') _d = _c - 48; \
else if(_c >= 'A' && _c <= 'F') _d = _c - 55; \
else if(_c >= 'a' && _c <= 'f') _d = _c - 87; \
else _d = -1; \
}while(0);
#define STRING2BYTES(_src, _dst, _dstlen) \
do {\
int h, l;\
int _srclen = strlen(_src); \
if(_srclen == 1) _dstlen = 1; else _dstlen = _srclen / 2; \
_dst = (char *)malloc(_dstlen + 1); \
if(!_dst) break; \
memset(_dst, '\0', _dstlen + 1); \
if(_srclen == 1) {CHAR2INT(_src[0], l); _dst[0] = (unsigned char)l; break;} \
for(int i=0, j=0; j<_srclen; i++, j+=2) { \
CHAR2INT(_src[j], h); CHAR2INT(_src[j + 1], l); \
_dst[i] = (unsigned char)(h << 4 | l); } \
}while(0);
#define FREESTRING2BYTES(_src) if(_src) free(_src); _src = NULL;
十六进制字符串转字节序列
最新推荐文章于 2023-11-27 15:08:24 发布