Correction de la correction, plantage en debug à l'exécution.
Ne compile pas sous VS2008...
Version corrigée :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <assert.h>
int main(void)
{
char const * const hex_string = "00112233445566778899AABBCCDDEEFF";
size_t hex_string_length = strlen(hex_string);
unsigned char* res = malloc(hex_string_length / 2);
size_t i ;
unsigned short int scchar ;
assert((hex_string_length % 2) == 0);
for(i = 0; i < hex_string_length; i += 2)
{
if(sscanf(hex_string + i, "%02cX", &scchar) == 0)
{
break; /* handle error */
} else
res[i / 2] = (unsigned char)scchar ;
}
if (res)
free(res);
return 0;
} |
EDIT : Re-corrigé sur le sscanf, débordement de mémoire (lecture unsigned short int => boum en debug sous VS).