hex_string_to_hex.c

输入:一串16进制的字串

输出:由字串转换出来的16进制数值

例如 "E5BC35" -->0xe5,0xbc,0x35

/********************************************************************
	created:	2012/02/21
	filename: 	hex_string_to_hex.c
	author:		
	
	purpose:	
*********************************************************************/

#include 
#include 
#include 
#include 

//-------------------------------------------------------------------

static int oneHexCharToHex(char h)
{
	int x = 0;
	if (isdigit(h)) {
		x = h - '0';
	} else if (isupper(h)) {
		x = h - 'A' + 10;
	} else {
		x = h - 'a' + 10;
	}
	return x;
}

/*Chaneg TEXT HEX such as E5BC35 to 
* E5BC35 -->0xe5,0xbc,0x35
* inStr must >0 otherwise return -1*/
int ChangeTHEX2CPTR(const char * inHexString, char * outHex)
{
	int success = -1;
	int len = 0;
	int i;
	char ch1, ch2;
	do 
	{
		if (NULL == inHexString || NULL == outHex) {
			printf("error: inHexString or outHex is null!\n");
			break;
		}
		len = strlen(inHexString);
		if (len <= 1) {
			printf("error: strlen(inHexString) <= 1!\n");
			break;
		}
		len &= ~1;
		for (i=0; i


你可能感兴趣的:(C)