0

How to convert the char array to Byte array?

char CardNumber[8] = "B763AB23"; // Length is 8, basically it's in Hex
                                 // B7 63 AB 23

I need to convert it into Byte array to byte CardNumberByte[4]; So basically, it should be like :

CardNumberByte[0] = B7;
CardNumberByte[1] = 63;
CardNumberByte[2] = AB;
CardNumberByte[3] = 23;

I am unable to find any solution for that.

1
  • The solution is here this will solve your problem. Click Here Commented Jun 24, 2017 at 11:13

2 Answers 2

0
union 
{
uint32_t number;
uint8_t CardNumberByte[4];
} CardNum;

char cn[] = "B763AB23";

and "conversion":

CardNum.Number = strtol(cn, NULL, 16);

and your bytes are avaiable via

CardNum.CardNumberByte[xx]

But I think you should start from the begginers C++ tutorial and learn some basics.

Sign up to request clarification or add additional context in comments.

Comments

0

Eight hex characters is 32 bits, so first put the number in a long (32-bit on the Arduino) value:

long number = (long) strtol(&CardNumber[0], NULL, 16);

Then bit-shift the values into bytes:

CardNumberByte[0] = number >> 24;
CardNumberByte[1] = number >> 16 & 0xFF;
CardNumberByte[2] = number >> 8 & 0xFF;
CardNumberByte[3] = number & 0xFF;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.