-2

I am working on a school project using Arduino, I have no past experience with C++, and I want to generate a unique MAC Address for every chip. Now I have built a function that creates a two dimensional char array containing the unique MAC. And it returns something like this:

// 2D char array example:
char mac[6][2] = {{'A', 'B'}, {'4', 'D'}, {'F', '5'}, {'C', '9'}, {'B', '7'}, {'F', '2'}};

And I want to convert it to something like this:

// Hex array example:
byte mac[6] = {0xAB, 0x4D, 0xF5, 0xC9, 0xB7, 0xF2};

Important Note: Arduino does not support STL so I need an implementation that does not use it.

How to achieve this result?

This is not a duplicate of this question.

Ameer Taweel
  • 949
  • 1
  • 11
  • 37
  • 1
    Hint `first_value_as_decimal * 16 + second_value_as_decimal` equals the the same value – NathanOliver Jan 14 '19 at 15:05
  • Possible duplicate of [Hex character to int in C++](https://stackoverflow.com/questions/6457551/hex-character-to-int-in-c) – paler123 Jan 14 '19 at 15:07
  • @NathanOliver Do you mean that: `byte mac[6] = {171, 77, 261, 201, 183, 258};` is the same array as above? – Ameer Taweel Jan 14 '19 at 15:08
  • @paler123 I can't use `STL` so it is not a duplicate. – Ameer Taweel Jan 14 '19 at 15:09
  • 1
    @AmeerTaweel 10 * 16 + 11 is not 21. – molbdnilo Jan 14 '19 at 15:11
  • @AmeerTaweel `A` as a decimal is 10, `B` is 11, 10 * 16 + 11 != 21. It is 171 – NathanOliver Jan 14 '19 at 15:12
  • @paler123 not an exact duplicate though the posting you reference provides the first step in the conversion. OP needs to do the conversion to hex of each character in the pairs then combine the two characters of a pair into a single byte. So after converting each of two characters into hex value next shift one of the resulting nibble values (0x0 - 0xf) to the upper nibble then bitwise OR the two values together. Shift by either multiple times 16 or do left shift with `<<` operator by 8. – Richard Chambers Jan 14 '19 at 15:12
  • @molbdnilo I edited the comment, is it right now? – Ameer Taweel Jan 14 '19 at 15:12
  • This array, is the same result that I want? `byte mac[6] = {171, 77, 261, 201, 183, 258};` – Ameer Taweel Jan 14 '19 at 15:13
  • 261 != F5 and 258 != F2. F5 = 15*16+5 – NathanOliver Jan 14 '19 at 15:14
  • @NathanOliver now I get it: `byte mac[6] = {171, 77, 245, 201, 183, 242};`, please post an answer so I can vote for you. – Ameer Taweel Jan 14 '19 at 15:17
  • Why wouldn't Arduino support the standard library? I use it there all the time. – Bartek Banachewicz Jan 14 '19 at 15:32
  • @BartekBanachewicz you can read this: https://arduino.stackexchange.com/questions/24790/is-the-c-stl-fully-supported-on-arduino – Ameer Taweel Jan 14 '19 at 15:37
  • @AmeerTaweel That just says it's not included in the Arduino IDE, noting about Arduino not supporting it. – gre_gor Jan 14 '19 at 16:13
  • @gre_gor Yeah, but I don't have time to manually include it, and my chip resources are limited so I don't want to import a lot of external libraries. – Ameer Taweel Jan 14 '19 at 17:43

1 Answers1

2
byte HexCharToByte(char c) {
    if (c >= '0' && c <= '9') {
        return c - '0';
    } else if (c >= 'A' && c <= 'F') {
        return c - 'A' + 10;
    } else if (c >= 'a' && c <= 'f') {
        return c - 'a' + 10;
    }
}

void TransformMac(char input[6][2], byte output[6]) {
    for (int i = 0; i < 6; ++i) {
        output[i] = (HexCharToByte(input[i][0]) << 4) | HexCharToByte(input[i][1]); 
    }
}
CrafterKolyan
  • 1,042
  • 5
  • 13