-3 c
将输入作为十六进制字符串,然后将其转换为C中的char字符串.十六进制字符串可以包含0x00,转换后转换为Ascii中的0.这会终止字符串.我必须将值存储在char字符串中,因为API使用它.
我的代码到目前为止:
int hex_to_int(unsigned char c) {
int first =0;
int second =0;
int result=0;
if(c>=97 && c<=102)
c-=32;
first=c / 16 - 3;
second =c % 16;
result = first*10 + second;
if(result > 9) result--;
return result;
}
unsigned char hex_to_ascii(unsigned char c, unsigned char d){
unsigned char a='0';
int high = hex_to_int(c) * 16;
int low = hex_to_int(d);
a= high+low;
return a;
}
unsigned char* HextoString(unsigned char *st){
int length = strlen((const char*)st);
unsigned char* result=(unsigned char*)malloc(length/2+1);
unsigned char arr[500];
int i;
unsigned char buf = 0;
int j=0;
for(i = 0; i < length; i++)
{
if(i % 2 != 0)
{
arr[j++]=(unsigned char)hex_to_ascii(buf, st[i]);
}
else
{
buf = st[i];
}
}
arr[length/2+1]='\0';
memcpy(result,arr,length/2+1);
return result;
}
Run Code Online (Sandbox Code Playgroud)
您可以将任何值存储在char数组中.但是如果要存储值0x00,则不能在此数组上使用字符串函数.因此,您必须使用整数变量来存储要存储的数据的长度.然后,您可以编写使用此整数的函数.