# Hex String to Byte Array

**URL:** <https://forum.arduino.cc/t/hex-string-to-byte-array/563827>\
**Category:** Programming\
**Created:** [December 24, 2018, 5:40pm UTC](https://forum.arduino.cc/t/hex-string-to-byte-array/563827 "2018-12-24T17:40:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![beanzapper](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/beanzapper/32/1171418_2.png) [@beanzapper](https://forum.arduino.cc/u/beanzapper)\
**Post date:** [December 24, 2018, 5:40pm UTC](https://forum.arduino.cc/t/hex-string-to-byte-array/563827/1 "2018-12-24T17:40:14Z")

</div>

Hello,

I'm having trouble figuring out how to convert a hex string such as "A489B1" into an array like [0xA4, 0x89, 0xB1].

Anyone have any ideas?

Thank you!

---

<div class="post-metadata">

**Author:** ![paulvha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulvha/32/1098795_2.png) [@paulvha](https://forum.arduino.cc/u/paulvha)\
**Post date:** [December 24, 2018, 6:49pm UTC](https://forum.arduino.cc/t/hex-string-to-byte-array/563827/3 "2018-12-24T18:49:42Z")

</div>

you could try something like this

```cpp
char buf[40];

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

void loop() {
 convert("ab4567");
 Serial.println(buf);
 
}

void convert(char *s)
{
 int i, j, k;
 buf[0] = 0x0;
 
 for (j = 0, i = 0, k = 0; j < strlen(s); j++)
 {
   
   if (i++ == 0) {
     buf[k++] = '0';
     buf[k++] = 'x';
   }
   
   buf[k++] = s[j];
   
   if (i == 2) {
     if(j != strlen(s) -1)  buf[k++] = ',';
     i = 0;
   }
 }

buf[k] = 0x0;

}

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [December 24, 2018, 11:50pm UTC](https://forum.arduino.cc/t/hex-string-to-byte-array/563827/4 "2018-12-24T23:50:08Z")

</div>

```cpp
// convert a hex string such as "A489B1" into an array like [0xA4, 0x89, 0xB1].

const byte MaxByteArraySize = 5;

void setup()
{
  Serial.begin(115200);

  byte byteArray[MaxByteArraySize] = {0};

  hexCharacterStringToBytes(byteArray, "A489B1");
  dumpByteArray(byteArray, MaxByteArraySize);

  hexCharacterStringToBytes(byteArray, "A489B10");
  dumpByteArray(byteArray, MaxByteArraySize);
}

void hexCharacterStringToBytes(byte *byteArray, const char *hexString)
{
  bool oddLength = strlen(hexString) & 1;

  byte currentByte = 0;
  byte byteIndex = 0;

  for (byte charIndex = 0; charIndex < strlen(hexString); charIndex++)
  {
    bool oddCharIndex = charIndex & 1;

    if (oddLength)
    {
      // If the length is odd
      if (oddCharIndex)
      {
        // odd characters go in high nibble
        currentByte = nibble(hexString[charIndex]) << 4;
      }
      else
      {
        // Even characters go into low nibble
        currentByte |= nibble(hexString[charIndex]);
        byteArray[byteIndex++] = currentByte;
        currentByte = 0;
      }
    }
    else
    {
      // If the length is even
      if (!oddCharIndex)
      {
        // Odd characters go into the high nibble
        currentByte = nibble(hexString[charIndex]) << 4;
      }
      else
      {
        // Odd characters go into low nibble
        currentByte |= nibble(hexString[charIndex]);
        byteArray[byteIndex++] = currentByte;
        currentByte = 0;
      }
    }
  }
}

void dumpByteArray(const byte * byteArray, const byte arraySize)
{

for (int i = 0; i < arraySize; i++)
{
  Serial.print("0x");
  if (byteArray[i] < 0x10)
    Serial.print("0");
  Serial.print(byteArray[i], HEX);
  Serial.print(", ");
}
Serial.println();
}

byte nibble(char c)
{
  if (c >= '0' && c <= '9')
    return c - '0';

  if (c >= 'a' && c <= 'f')
    return c - 'a' + 10;

  if (c >= 'A' && c <= 'F')
    return c - 'A' + 10;

  return 0;  // Not a valid hexadecimal character
}

void loop() {}

```

---

<div class="post-metadata">

**Author:** ![beanzapper](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/beanzapper/32/1171418_2.png) [@beanzapper](https://forum.arduino.cc/u/beanzapper)\
**Post date:** [December 25, 2018, 2:55am UTC](https://forum.arduino.cc/t/hex-string-to-byte-array/563827/5 "2018-12-25T02:55:35Z")

</div>

Thank you John, that's great!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:32pm UTC](https://forum.arduino.cc/t/hex-string-to-byte-array/563827/6 "2021-05-05T23:32:25Z")

</div>


