Converting ASCII to hex

Hello,
I'm trying to convert a ascii representation of a mac address like 1122334455667788 to eight bytes of hex that represents the same mac address. This question has been asked before on this forum, but I haven't had success getting their solutions to work.

I tried post #2 here which seem promising, but did not work.
https://forum.arduino.cc/index.php?topic=123309.0

My source is a char[17] with the last byte null terminated. I'm trying to stuff it into a char[8], but I know it's actually hex values not characters.

I'm trying to convert a ascii representation of a mac address like 1122334455667788

That means, you have data like this --

char myArray[17] = {'1', '1', '2', '2', '3', '3', '4', '4', '5', '5', '6', '6', '7', '7', '8', '8', '\0'}; //Is it correct?

or

char myArray[17] = {
0x31, 0x31, 0x32, 0x32, 0x33, 0x33, 0x34, 0x34, 0x35, 0x35, 
0x36, 0x36, 0x37, 0x37, 0x38, 0x38, 0x00
                            };   //Is it correct?

Now, you want like this --

byte nyData[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; //Is it correct?

Say, your data is --

[code]char myArray[17] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '\0'}

Now, which one of the following you want?

byte nyData[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};

or

byte nyData[8] = {0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE};

OP... you do realise the contents of these are exactly the same.... don't you?

char myArray[17] = {'1', '1', '2', '2', '3', '3', '4', '4', '5', '5', '6', '6', '7', '7', '8', '8', '\0'}; 

-  and  -

char myArray[17] = {
           0x31, 0x31, 0x32, 0x32, 0x33, 0x33, 0x34, 0x34, 0x35, 0x35, 
           0x36, 0x36, 0x37, 0x37, 0x38, 0x38, 0x00  };

arusr:
I'm trying to convert a ascii representation of a mac address like 1122334455667788 to eight bytes of hex that represents the same mac address.

char text[] = "1122334455667788";
char hex[8];

void foo() {
  for (int i = 0; i < 8; i++) {
    hex[i] = (toHex(text[i * 2]) << 8) | toHex(text[i * 2 + 1]);
  }
}
inline byte toHex(char z) {
  return z <= '9' ? z - '0' :  z <= 'F' ? z - 'A' + 10 : z - 'a' + 10;
}

@ PaulMurrayCbr

Your solution of Post#3 is expected to provide what OP wants --

OP's Requirement Your solution Provides
hex[0] = 11 hex[0] = 1
hex[1] = 22 hex[1] = 2
...........................................

Will you please look into your Post#3?

So change the

  hex[i] = (toHex(text[i * 2]) << 8) | toHex(text[i * 2 + 1]);

to

  hex[i] = (toHex(text[i * 2]) << 4) | toHex(text[i * 2 + 1]);
char text[] = "1122334455667788";
char hex[8];

void foo() {
  for (int i = 0; i < 8; i++) {
    hex[i] = (toHex(text[i * 2]) << 4) | toHex(text[i * 2 + 1]);
  }
}
inline byte toHex(char z) {
  return z <= '9' ? z - '0' :  z <= 'F' ? z - 'A' + 10 : z - 'a' + 10;
}

void setup() {
  Serial.begin(250000);
  foo();
  dump(&text, sizeof(text));
  dump(&hex, sizeof(hex));
}

void loop() {}

void dump(const void* adrIn, int len) {
  byte* adr = (byte*) adrIn;
  byte idx;
  byte blanks;
  if (len) {
    for (; len > 0; len -= 16, adr += 16) {
      phAdr(adr);
      for (idx = 0; idx < 16; idx++) {
        if (idx < len ) {
          byte curr = adr[idx];
          phByte(curr);
          blanks = 1;
        } else {
          blanks = 3;
        }
        while (blanks--) {
          Serial.write(' ');
        }
      }
      Serial.write('\'');
      for (idx = 0; (idx < 16) && (idx < len); idx++) {
        byte curr = adr[idx];
        Serial.write(curr < 0x20 ? '.' : curr);
      }
      Serial.write('\'');
      Serial.println();
    }
  }
}

void phByte(byte value) {
  if (value < 16) {
    Serial.write('0');
  }
  Serial.print(value, HEX);
}

void phAdr(const byte* adr) {
  phByte((byte)(((uint16_t)adr) >> 8));
  phByte((uint16_t)adr);
  Serial.write(':');
  Serial.write(' ');
}
0200: 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 '1122334455667788'
0210: 00                                              '.'
0226: 11 22 33 44 55 66 77 88                         '."3DUfwˆ'

0200: 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 '1122334455667788'
0210: 00 '.'
0226: 11 22 33 44 55 66 77 88 '."3DUfwˆ'

How have you dump this output on the Screen (the Serial Monitor) -- I would like to now.

GolamMostafa:
How have you dump this output on the Screen (the Serial Monitor) -- I would like to now.

With the sketch included in the quoted post.

With the sketch included in the quoted post.

I have found it. Thanks+.

Oh, I see. I didn't really think about my question thoroughly, because those other options didn't occur to me. I want:

byte nyData[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};

And thanks to @PaulMurrayCbr and @GolamMostafa. The code (and the fix) does what I wanted. Much appreciated.

GolamMostafa:

Whandall:
So change the

PaulMurrayCbr:

char text[] = "1122334455667788";

char hex[8];
...