5

How to convert the char array to Byte array? I tried every method but it doesn't work.

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.

2
  • 1
    "B763AB23" is actually 9 bytes. You forgot the null terminator. And B7, 63, AB, 23 are not valid hexadecimal integer literals. They should be 0xB7, 0x63, 0xAB, 0x23.
    – gre_gor
    Jun 25, 2017 at 20:42
  • Related: convert String Hex to Hex byte Aug 6, 2020 at 1:34

3 Answers 3

2

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() 
{
}
1
  • Note that this is limited to 0xFFFFFFFF, or 4 bytes. Apr 21, 2021 at 10:47
0

Here is a simple solution to your question. I took each character, checked against its hexadecimal character and returned as a combination.

char CardNumber[] = "B763AB23";
byte CardNumberByte[4];

void setup() {
  Serial.begin(9600);
}

void loop() {
  for(char i = 0; i < 4; i++)
{
  byte extract;
  char a = CardNumber[2*i];
  char b = CardNumber[2*i + 1];
  extract = convertCharToHex(a)<<4 | convertCharToHex(b);
  CardNumberByte[i] = extract;
  Serial.println(CardNumberByte[i], HEX);
}

  while(1);
}



char convertCharToHex(char ch)
{
  char returnType;
  switch(ch)
  {
    case '0':
    returnType = 0;
    break;
    case  '1' :
    returnType = 1;
    break;
    case  '2':
    returnType = 2;
    break;
    case  '3':
    returnType = 3;
    break;
    case  '4' :
    returnType = 4;
    break;
    case  '5':
    returnType = 5;
    break;
    case  '6':
    returnType = 6;
    break;
    case  '7':
    returnType = 7;
    break;
    case  '8':
    returnType = 8;
    break;
    case  '9':
    returnType = 9;
    break;
    case  'A':
    returnType = 10;
    break;
    case  'B':
    returnType = 11;
    break;
    case  'C':
    returnType = 12;
    break;
    case  'D':
    returnType = 13;
    break;
    case  'E':
    returnType = 14;
    break;
    case  'F' :
    returnType = 15;
    break;
    default:
    returnType = 0;
    break;
  }
  return returnType;
}
2
  • 1
    That is very kind of you to provide a working sketch. I hope you don't mind that I have a few positive comments. You give CardNumber a string of 9 bytes (with the zero-terminator it is 9 bytes) and CardNumber has only 8 bytes. The switch-case statement is long, when there is an error in there, it can be overlooked. Therefor a short calculation would be preferred.
    – Jot
    Jun 24, 2017 at 12:49
  • yes, no problem. I only provided a simple working copy, not necessarily compact. Yours is a good one though. Jun 24, 2017 at 12:52
0

A char array is a byte arrray. No difference whatsoever

What you are attempting to do is a conversion of hex string to byte. For that you have to determine if you are scanning left to right or right to left.

After that convert a char in the string to a half byte.

From there you shift the half byte left and or it with the next half byte until the whole string has been exhausted.

2
  • This is a great explanation. Now we just need to see you do it in code. Then, I'll upvote it. Aug 6, 2020 at 1:14
  • Keep in mind you need to make sure to also convert from ASCII chars to hex/binary. A 0 in ASCII is a decimal 48 or hex 30, so you must subtract 48 from it to be a true hex zero. You must do this for all the ASCII chars, taking care to also account for the fact that you might see the ASCII "hex" chars in lower or uppercase A through F, and lowercase must be handled differently from uppercase since they are different decimal values. Aug 6, 2020 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.