I was trying to think of a library function to do this, but couldn't really think of any. Perhaps something like this would work for you:
Code:
size_t hex_string_to_hex_data( char const* txt, void* dst, size_t max )
{
unsigned char
nyb,
* cvt = ( unsigned char* )dst;
size_t
cnt,
idx,
len = strlen( txt ),
hlf = len / 2;
if( len == 0 || len % 2 != 0 || hlf > max )
return 0;
for( idx = 0; idx < len; idx += 2, ++cvt )
{
*cvt = 0;
for( cnt = 0; cnt < 2; ++cnt )
{
nyb = tolower( txt[ idx + cnt ] );
if( isdigit( nyb ) )
nyb -= '0';
else if( nyb >= 'a' && nyb <= 'f' )
nyb = ( nyb - 'a' ) + 10;
else
return 0;
*cvt |= nyb << ( ( 1 - cnt ) << 2 );
}
}
return hlf;
}