C语言字符数组如何转成16进制

char pKey = “AA1F1905A8F3E4D3C3C536FE615C93BC”
char Key[16] = { 0xAA,0x1F,0x19,0x05,0xA8,0xF3,0xE4,0xD3,0xC3,0xC5,0x36,0xFE,0x61,0x5C,0x93,0xBC };
我想有pKey转换为Key,如何做?
字符串AA转换为16进制0xAA

int str2mem(char* pKey, char* pBuffer)
{
int i;
//unsigned int temp=0;
unsigned int temp;
for (i = 0; i<0x10; ++i)
{
sscanf(pKey + 2 * i, "%02X", &temp);
pBuffer[i] = (char)temp;
}

return 0;
}
这个比较好

需要准备的材料分别有:电脑、C语言编译器。

1、首先,打开C语言编译器,新建一个初始.cpp文件,例如:test.cpp。

2、在test.cpp文件中,输入C语言代码:

char *pKey = "AA1F1905A8F3E4D3C3C536FE615C93BC";

for (int i = 0; i < strlen(pKey) - 1; i += 2) {

printf("0x%c%c\n", pKey[i], pKey[i+1]);

}

3、编译器运行test.cpp文件,此时成功将字符串数组转为了16进制的形式进行了输出。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-08-10
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main(void) 
{
char *pKey = "AA1F1905A8F3E4D3C3C536FE615C93BC";
int n=strlen(pKey)/2,i;
char *Key=(char *)malloc(sizeof(char)*n);
for(i=0;i<n;++i)
{
sscanf(pKey+2*i,"%2X",Key+i);
}
for(i=0;i<n;++i)
{
printf("%#02hhX ",Key[i]);
}
return 0;
}

追问

Key会写越界

本回答被提问者和网友采纳
相似回答