There are indeed many methods (and they all work if done right).
Is it always a lenght of 8 ? That means it is 32-bit unsigned long number.
The strtoul can convert it to a long. Use '16' for the base. If you need the seperate numbers, you can shift the unsigned long and convert to bytes or use a union.
It is also possible to do with a for statement can convert each character of the input to a value: forum.arduino.cc: convert HEX (ASCII) to a DEC int
[ADDED]
Thank you @goddland_16 for providing a full sketch. I could not stay behind and wrote a sketch with strtoul.
char *CardNumber = "B763AB23";
byte CardNumberByte[4];
void setup()
{
Serial.begin(9600);
Serial.println("With strtoul");
// Use 'nullptr' or 'NULL' for the second parameter.
unsigned long number = strtoul( CardNumber, nullptr, 16);
for(int i=3; i>=0; i--) // start with lowest byte of number
{
CardNumberByte[i] = number & 0xFF; // or: = byte( number);
number >>= 8; // get next byte into position
}
for(int i=0; i<4; i++)
{
Serial.print("0x");
Serial.println(CardNumberByte[i], HEX);
}
}
void loop()
{
}
"B763AB23"
is actually 9 bytes. You forgot the null terminator. AndB7
,63
,AB
,23
are not valid hexadecimal integer literals. They should be0xB7
,0x63
,0xAB
,0x23
.