This question already has an answer here:
- How to convert a char array to a byte array? 3 answers
I need to convert byte array which is in hex to String.
For example:
byte array[4] = {0xAB, 0xCD, 0xEF, 0x99};
//array[0] = 0xAB;
//array[1] = 0xCD;
//array[2] = 0xEF;
//array[3] = 0x99;
Convert above to :
char number[9]; //Should be "ABCDEF99"
I have done it vice versa. That is convert char array to byte array
char CardNumber[9] = "ABCDEF99";
byte j;
auto getNum = [](char c)
{
return c > '9' ? c - 'a' + 10 : c - '0';
};
char arr[10];
char i;
byte *ptr = out;
for (i = 0; i < 8; i++)
{
arr[i] = CardNumber[i];
}
for (char *index = arr ; *index ; ++index, ++ptr )
{
*ptr = (getNum( *index++ ) << 4) + getNum(*index);
}
//Check converted byte values.
Serial.print("Card Number in Bytes :");
for (j = 0; j < 4; j++)
{
Serial.print(out[j], HEX );
}
Serial.println();