How to convert ASCII/UNICODE string to hex string? Function/API to convert to hex string

Following is code snippet that converts an UNICODE/WIDE string to HEX string:

1
2
3
4
5
6
7
8
9
10
11
	WCHAR str[] = L"abcd"; // string to be converted to HEX string
        WCHAR hexStr[MAX_PATH]; // string to store HEX string
	char *temp = (char *)str;
	for(i = 0; i < wcslen(str)*2; i++ )
	{
		if (temp[i])
			_itow_s (temp[i], hexStr, 3, 16);
		else
			wcscat_s(hexStr, 3, L"00");
		hexStr += 2;
	};

Following is code snippet that converts an ASCII string to HEX string:

1
2
3
4
5
6
7
8
9
10
11
	char str[] = L"abcd"; // string to be converted to HEX string
        char hexStr[MAX_PATH]; // string to store HEX string
	char *temp = str;
	for(i = 0; i < strlen(str); i++ )
	{
		if (temp[i])
			_itoa_s (temp[i], hexStr, 3, 16);
		else
			strcat_s(hexStr, 3, "00");
		hexStr += 2;
	};
 

Recent Entries

Leave a Reply