static void asc2bin (u_int8_t *bin, u_int8_t *asc, u_int16_t len)
{
  int i;

  for (i = 0; i < len; i += 2, asc += 2)
    {
      *bin++ = hexchar2bin(asc);
    }
}
int hex2bin2(const char *hex, unsigned char *buf, size_t *len)
{
	size_t count = 0;
	char   debugString[2048];

	while (*hex != '\0' && count < *len)
	{
		// skip all that is not a hex char
		while (!ISHEXCHAR(*hex) && *hex != '\0')
			hex++;
		if (*hex == '\0')
			break;

		// The nex char should be a hex char too
		if (ISHEXCHAR(hex[1]))
			buf[count++] = (unsigned char)(16 * hexchar2bin(*hex) + hexchar2bin(hex[1]));
		else
		{
			printf("ERR: invalid input in hex string: expected a hex char after '%c'\n", *hex);
			sprintf_s(debugString, 2047, "ERR: invalid input in hex string: expected a hex char after '%c'\n", *hex);
			DebugMessage(debugString);
			return -1;
		}

		hex += 2;
	}

	if (count >= *len)
	{
		printf("ERR: hex string too large (should be max %d bytes\n", (int) *len);
		sprintf_s(debugString, 2047, "ERR: hex string too large (should be max %d bytes\n", *len);
		DebugMessage(debugString);
		return -1;
	}

	*len = count;

	return 0;
}