C / C ++ function hexadecimal string transfer byte stream

void HexStrToByte(const char* source, size_t sourceLen, void* dest)
{
	int highByte, lowByte; size_t i;
	for (i = 0; i < sourceLen; i += 2)
	{
		highByte = (int)source[i];
		lowByte = (int)source[i + 1];
		/*转换高位*/
		if (highByte > 0x39)
		{
			if (highByte < 0x47)
				highByte -= 0x37;//大写
			else
				highByte -= 0x57;//小写
		}
		else
			highByte -= 0x30;
		/*转换低位*/
		if (lowByte > 0x39)
		{
			if (lowByte < 0x47)
				lowByte -= 0x37;
			else
				lowByte -= 0x57;
		}
		else
			lowByte -= 0x30;
		((char *)dest)[i / 2] = (char)((highByte << 4) | lowByte);
	}
}

source for the string pointer
sourceLen a string length strlen function can be used to obtain
dest is the result buffer, at least (sourceLen / 2) space, in order to ensure that no cross-border memory
this version is based on a modified version of others, no compilation warnings, compatibility with older C translater

Published an original article · won praise 0 · Views 104

Guess you like

Origin blog.csdn.net/u011486334/article/details/104090766