user-0 提问: 如何将字符串转化成16进制字符数据

回复:
user: user-1
content: 引用 9 楼 mymtom 的回复: #include <stdio.h> #include <string.h>  unsigned char t[256] = {      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,      0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };  int s2h(const char *s, char *h) {     int r, i, n;     char *p, q;      p = h;     n = strlen(s);     if (n % 2) {         h[0] = t[(unsigned char)s[0]];         ++p;         ++s;     }      for (i = 0; i < n; i = i + 2) {         *(p++) = (t[(unsigned char)s[i]] << 4) | t[(unsigned char)s[i + 1]];     }      return (n + 1) / 2; }  int main(int argc, char *argv[]) {     char s[] = "a4e56EF";     char h[1024];     int i, n;      n = s2h(s, h);     for (i = 0; i < n; ++i) {         printf("%02x", (unsigned char)h[i]);     }     printf("\n");      return 0; }   這麼先進樓主居然不給分不識貨

user: user-2
content: 参考一下 #include <stdio.h> int main() {   char const* p = "123456";   unsigned char res[3];   sscanf(p, "%hhx%hhx%hhx", res, res + 1,res + 2);    printf("%#hhx %#hhx %#hhx", res[0], res[1], res[2]); }

user: user-1
content: 忘了，所有的 TEXT('0') 改成 '0'。

user: user-1
content: 也可以将函数改写为返回值的方式：#include <stdio.h> #include <stdlib.h>   /*  * lpSrcStr  : 要转换的字符串，如 “123456AbCdEf”。  * lpRetBytes: 要存放转换结果的字节数组。  * lpRetSize : lpRetBytes 数组的元素个数  * 返回：写入 lpRetBytes 的元素个数  */ size_t StrToHex(const char lpSrcStr[], unsigned char lpRetBytes[], size_t uiBuffSize) {     size_t n = 0;     if (lpSrcStr != NULL && lpRetBytes != NULL && uiBuffSize > 0) {         size_t uiLength = strlen(lpSrcStr);         if (uiLength % 2 == 0) {             size_t i = 0;             while (*lpSrcStr != 0 && (n = ((i++) >> 1)) < uiBuffSize) {                 lpRetBytes[n] <<= 4;                 if (*lpSrcStr >= TEXT('0') && *lpSrcStr <= TEXT('9')) {                     lpRetBytes[n] |= *lpSrcStr - '0';                 } else if (*lpSrcStr >= TEXT('a') && *lpSrcStr <= TEXT('f')) {                     lpRetBytes[n] |= *lpSrcStr - 'a' + 10;                 } else if (*lpSrcStr >= TEXT('A') && *lpSrcStr <= TEXT('F')) {                     lpRetBytes[n] |= *lpSrcStr - 'A' + 10;                 }                 lpSrcStr++;             }         }     }     return n; }   int main(int argc, char *argv[]) {     char str[] = "123456ABCDEF";     unsigned char ret[32];     size_t size = StrToHex(str, ret, _countof(ret));     for (int i = 0; i < size; i++) {         printf("%02X", ret[i]);     }     putchar('\n');     return 0; }

user: user-1
content: 帮你写了一个： #include <stdio.h> #include <stdlib.h>  /*  * lpSrcStr  : 要转换的字符串，如 “123456ABCDEF”。  * lpRetBytes: 要存放转换结果的字节数组。  * lpRetSize : 输入时，指定 lpRetBytes 的大小；调用结束后为 lpRetBytes 的个数。  */ void StrToHex(const char lpSrcStr[], unsigned char lpRetBytes[], size_t *lpRetSize) {     if (lpSrcStr != NULL && lpRetBytes != NULL && lpRetSize != NULL) {         size_t uiLength = strlen(lpSrcStr);         if (uiLength % 2 == 0) {             size_t i = 0;             size_t n = 0;             while (*lpSrcStr != 0 && (n = ((i++) >> 1)) < *lpRetSize) {                 lpRetBytes[n] <<= 4;                 if (*lpSrcStr >= TEXT('0') && *lpSrcStr <= TEXT('9')) {                     lpRetBytes[n] |= *lpSrcStr - '0';                 } else if (*lpSrcStr >= TEXT('a') && *lpSrcStr <= TEXT('f')) {                     lpRetBytes[n] |= *lpSrcStr - 'a' + 10;                 } else if (*lpSrcStr >= TEXT('A') && *lpSrcStr <= TEXT('F')) {                     lpRetBytes[n] |= *lpSrcStr - 'A' + 10;                 }                 lpSrcStr++;             }             *lpRetSize = n;         }     } }  int main(int argc, char *argv[]) {     char str[] = "123456ABCDEF";     unsigned char ret[32];     size_t size = _countof(ret);     StrToHex(str, ret, &size);     for (int i = 0; i < size; i++) {         printf("%02X", ret[i]);     }     putchar('\n');     return 0; }

user: user-3
content: #include <stdio.h> #include <string.h>  unsigned char t[256] = {      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,      0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };  int s2h(const char *s, char *h) {     int r, i, n;     char *p, q;      p = h;     n = strlen(s);     if (n % 2) {         h[0] = t[(unsigned char)s[0]];         ++p;         ++s;     }      for (i = 0; i < n; i = i + 2) {         *(p++) = (t[(unsigned char)s[i]] << 4) | t[(unsigned char)s[i + 1]];     }      return (n + 1) / 2; }  int main(int argc, char *argv[]) {     char s[] = "a4e56EF";     char h[1024];     int i, n;      n = s2h(s, h);     for (i = 0; i < n; ++i) {         printf("%02x", (unsigned char)h[i]);     }     printf("\n");      return 0; }

user: user-4
content: 仅供参考#include <stdio.h> #include <stdlib.h> #include <conio.h> unsigned int iv,i; unsigned char BCD[5];//定长10位BCD码 unsigned char sv[11]; void main() {     iv=123456789;//最大10位无符号正整数     //INT2BCD     sprintf(sv,"%010u",iv);     for (i=0;i<10;i+=2) {         BCD[i/2]=(sv[i]<<4)|(sv[i+1]&0x0F);     }     printf("BCD=%02x%02x%02x%02x%02x\n",BCD[0],BCD[1],BCD[2],BCD[3],BCD[4]);     //BCD2INT     for (i=0;i<10;i+=2) {         sv[i]='0'|(BCD[i/2]>>4);         sv[i+1]='0'|(BCD[i/2]&0x0F);     }     sscanf(sv,"%010u",&iv);     printf("iv=%010u\n",iv);     getch(); }

user: user-5
content: const DWORD TEM_LEN = 10; 	const DWORD TEM2_LEN = 10;  	char tmp[TEM_LEN] = "1234567"; 	unsigned char tmp2[TEM2_LEN] = {0, 0, 0};  	if (TEM2_LEN > strlen(tmp) / 2) 	{ 		int i = 0; 		while (true) 		{ 			if (i < strlen(tmp)) 			{ 				if (i + 1 < strlen(tmp)) 				{ 					if ('0' <= tmp[i] && tmp[i] <= '9' &&  						'0' <= tmp[i + 1] && tmp[i + 1] <= '9') 					{ 						tmp2[i / 2] = (tmp[i] - '0') * 16 + (tmp[i + 1] - '0'); 					} 					else 					{ 						tmp2[i / 2] = 0; 						break; 					} 				} 				else 				{ 					tmp2[i / 2] = tmp[i] - '0'; 					break; 				} 			} 			else 			{ 				break; 			}  			i = i + 2; 		} 	}

user: user-6
content: 如果不需要自己写转换代码，那么考虑使用库函数先转为2进制，然后把2进制数据格式化为字符串。

user: user-7
content: String2Hex(const std::string &str, unsigned char *buf, unsigned int &len) { 	unsigned char OneData = 0; 	len = 0; 	int ChL = 0; 	for (int i = 0; i < str.length(); i++) 	{ 		char C = static_cast<char>(str.at(i)); 		if (C == ' ') 		{ 			if (ChL != 0) 			{ 				buf[len++] = OneData; 				ChL = 0; 			}			 			continue; 		} 		 		OneData <<= 4; 		OneData += Char2Hex(C);		 		ChL++; 		if (ChL == 2) 		{ 			buf[len++] = OneData; 			ChL = 0; 		} 	} }   unsigned char Char2Hex(const char C) { 	unsigned char Data; 	if (C >= '0' && C <= '9') 	{     	Data = C - '0'; 	} 	else if (C >= 'A' && C <= 'Z') 	{         Data = C - 'A' + 10; 	} 	else if (C >= 'a' && C <= 'z') 	{         Data = C - 'a' + 10; 	} 	return Data; }