Convert HEX string to BYTE array, bug.

less than 1 minute read

Find the bug in the following code and win a prize. Its obvious once you find it. CSharp programmers are asked not to mock c++ in this thread.


#include "stdafx.h"
#include <string.h>
#include <stdio.h>

#define USHORT	unsigned short 
#define BYTE	unsigned char 

USHORT ConvertHexToBytes( 
		/* IN  */ char * hex, 
		/* IN  */ const USHORT hex_length, 
		/* OUT */ BYTE * buffer, 
		/* OUT */ const USHORT buffer_length, 
		/* IN  */ const char * delemnator = ":" )
{
	if( hex	== NULL || hex_length <= 0 ||
		buffer == NULL || buffer_length <= 0 )
	{
		return 0; 
	}

	char *next_token = NULL;
	USHORT iOffset = 0 ; 
	char *p = strtok_s(hex, delemnator, &next_token);
	while (p) {
		if( 1 != sscanf_s( p, "%02X", &buffer[ iOffset ] ) ) {
			break; 
		}
		iOffset++;
		if( iOffset >= buffer_length ) {
			break; // No more room. 
		}
		p = strtok_s(NULL, delemnator, &next_token);
	}
	return iOffset ; 
}

Leave a comment