Suppose I have these variables,
const uint8_t ndef_default_msg[33] = {
0xd1, 0x02, 0x1c, 0x53, 0x70, 0x91, 0x01, 0x09,
0x54, 0x02, 0x65, 0x6e, 0x4c, 0x69
I hope that can help you
/*
* DESCRIPTION
* Converts a block in ASCII representation to binary
* PARAMETERS
* char * inMessage : message in ASCII format, '\0' terminated
* OUTPUTS
* uint8 * outMessage : output message in binary format
* Format: outMessage[i], where i is byte number
* RETURN VALUE
* uint32 : number of converted bytes
*/
uint32 ascii2hex_block( uint8 * outMessage, int32 out_len, const char * inMessage )
{
#define SET_BIT(U,N) ((U) |= (0x1 << (N)))
int32 i = 0;
int32 k = 0;
int32 blockLen = 0;
char inChar;
uint8 hexVal;
uint32 retVal = 0;
while ( inMessage[blockLen]!='\0' ) blockLen++;
blockLen = blockLen >> 1;
if (blockLen <= out_len) // not enough space in output
{
retVal = blockLen;
for (i = 0; i < blockLen; i++)
{
outMessage[i] = 0;
inChar = inMessage[k];
hexVal = ascii2hex( inChar );
if (hexVal == 0xff) retVal = 0; // found an invalid character
if ( (hexVal & (0x1 << 0) ) != 0 ) SET_BIT( outMessage[i], 4 );
if ( (hexVal & (0x1 << 1) ) != 0 ) SET_BIT( outMessage[i], 5 );
if ( (hexVal & (0x1 << 2) ) != 0 ) SET_BIT( outMessage[i], 6 );
if ( (hexVal & (0x1 << 3) ) != 0 ) HELPER_SET_BIT( outMessage[i], 7 );
k++;
inChar = inMessage[k];
hexVal = ascii2hex( inChar );
if ( (hexVal & (0x1 << 0) ) != 0 ) SET_BIT( outMessage[i], 0 );
if ( (hexVal & (0x1 << 1) ) != 0 ) SET_BIT( outMessage[i], 1 );
if ( (hexVal & (0x1 << 2) ) != 0 ) SET_BIT( outMessage[i], 2 );
if ( (hexVal & (0x1 << 3) ) != 0 ) SET_BIT( outMessage[i], 3 );
k++;
}
}
return retVal;
}
And ascii2hex is defined as follow:
/*
* DESCRIPTION
* Converts an ascii character ('0'..'f' or '0'..'F') to corresponding integer value.
* In case of invalid ascii character, return value is 0xff
* USAGE
* uint8 ascii2hex( char inASCII );
* PARAMETERS
* char inASCII : ascii character to convert
* RETURN VALUE
* uint8 : value of inASCII, 0xff for invalid input
*/
uint8 ascii2hex( char inASCII )
{
uint8 retHex=0xff;
if( (inASCII>=48) && (inASCII<=57) )
retHex = inASCII-48;
else if( (inASCII>=65) && (inASCII<=70) )
retHex = inASCII-55;
else if( (inASCII>=97) && (inASCII<=102) )
retHex = inASCII-87;
return retHex;
}