将16进制的字符串转换为对应的字符

在C的编程中, 有时需要将16进制的ASCII码字符串转换为对应的字符串并显示出来。这里是我参考网上代码写的转换程序:

该程序分为两个部分:

一、先将对应的16进制的ASCCII字符串转换为对应的字符串

int Hex2Str(char * Hex,char * Str)
{
	int high = 0,low = 0;
	int temp = 0;

	if(NULL==Hex || NULL==Str){
		printf("the Hex or Str is empty\n");
		return -1;
	}

	if((strlen(Hex)&1)!=0){
		printf("the input Hex is wrong\n");
		return -2;
	}

	while(*Hex){
		high = HexChar2Value(*Hex);
		if(high<0){
		*Str = '\0';
		printf("the high of Hex can not be converted to value\n");
		return -3;
		}
		Hex++;
		low = HexChar2Value(*Hex);
		if(low<0){
		*Str= '\0';
		printf("the low of Hex can not be converted to value\n");
		return -4;
		}
		temp = (high<<4) + low;
		*Str = (char)temp;
		Str++;
		Hex++;
	}
	*Str = '\0';
	return 0;	
}

二、上述代码中用到了将16进制的字符转换为对应的int型的数字

int HexChar2Value(const char HexChar)
{
	int result = 0;
	if(HexChar>='0' && HexChar <='9'){
		result = (int)(HexChar - '0');
	}
	else if(HexChar>='a' && HexChar <='f'){
			result = (int)(HexChar - 'a')+10;
		}
		else if(HexChar>='A' && HexChar<='F'){
				result = (int)(HexChar - 'A') + 10;
			}
		else {
			printf("fail to convert HexChar to Value\n");
			return -1;
		}
		return result;
}
程序的全图如下:

# include
# include
# include


int HexChar2Value(const char HexChar)
{
int result = 0;
if(HexChar>='0' && HexChar <='9'){
result = (int)(HexChar - '0');
}
else if(HexChar>='a' && HexChar <='f'){
result = (int)(HexChar - 'a')+10;
}
else if(HexChar>='A' && HexChar<='F'){
result = (int)(HexChar - 'A') + 10;
}
else {
printf("fail to convert HexChar to Value\n");
return -1;
}
return result;
}


int Hex2Str(char * Hex,char * Str)
{
int high = 0,low = 0;
int temp = 0;


if(NULL==Hex || NULL==Str){
printf("the Hex or Str is empty\n");
return -1;
}


if((strlen(Hex)&1)!=0){
printf("the input Hex is wrong\n");
return -2;
}

while(*Hex){
high = HexChar2Value(*Hex);
if(high<0){
*Str = '\0';
printf("the high of Hex can not be converted to value\n");
return -3;
}
Hex++;
low = HexChar2Value(*Hex);
if(low<0){
*Str= '\0';
printf("the low of Hex can not be converted to value\n");
return -4;
}
temp = (high<<4) + low;
*Str = (char)temp;
Str++;
Hex++;
}
*Str = '\0';
return 0;
}


int main()
{
char *a = "4f4b";
char b[512];

Hex2Str(a,b);
printf("%s\n",b);


return 0;
}


PS:第二步的功能与atoi有点类似,但atoi只能用来转换0到9的字符,无法转换A到F的字符

上述的程序的使用范围有一定的局限性,其仅适用于字符串,无法用于其他数据类型的转换,故编写了另一个程序:

static int HexChar2Value(const char HexChar)
{
	int result = 0;
	//NbwfPrintf("HexChar2Value     %c\n",HexChar);
	if(HexChar>='0' && HexChar<='9'){
		result = (int)(HexChar - '0');
	//	NbwfPrintf("HexChar2Value    %d\n",result);
	}
	else{
			 if(HexChar>='a' && HexChar <='f'){
				result = (int)(HexChar - 'a')+10;
			}
			else {
					if(HexChar>='A' && HexChar<='F'){
						result = (int)(HexChar - 'A') + 10;
					}
					else {
						printf("fail to convert HexChar to Value\n");
						return -1;
					}
		}
	}
		return result;
}
static int Hex2Str(char * Hex,char * Str,char Hex_Len)
{
	int high = 0,low = 0;
	int temp = 0;
	int temp_len = (int)Hex_Len;
	

	if(NULL==Hex || NULL==Str){
		printf("the Hex or Str is empty\n");
		return -1;
	}

	if((temp_len&1)!=0){
		printf("the input Hex is wrong\n");
		return -2;
	}
	temp_len = temp_len/2;
	while(temp_len--){
		high = HexChar2Value(*Hex);
		if(high<0){
		*Str = '\0';
		printf("the high of Hex can not be converted to value\n");
		return -3;
		}
		Hex++;
		low = HexChar2Value(*Hex);
		if(low<0){
		*Str= '\0';
		printf("the low of Hex can not be converted to value\n");
		return -4;
		}
		temp = (high<<4) + low;
		*Str = (char)temp;
		Str++;
		Hex++;
	}
	*Str = '\0';
	return 0;	
}

该程序与之前程序的最大区别在与转换的结束是以传入的长度来结束而不是字符的NULL标志来结束,从而提高了其使用范围。



你可能感兴趣的:(C语言)