String HEX to Dec

Hi!
Im looking for a conversion, but i still cannot find the right way. I read the data to a string, and this string is going to be the MAC address, but i still cannot find a way to convert it properly. This is my code:

GivenMacAddress[0] = String(readedData[21]) + String(readedData[22]);
            GivenMacAddress[1] = String(readedData[23]) + String(readedData[24]);
            GivenMacAddress[2] = String(readedData[25]) + String(readedData[26]);
            GivenMacAddress[3] = String(readedData[27]) + String(readedData[28]);
            GivenMacAddress[4] = String(readedData[29]) + String(readedData[30]);
            GivenMacAddress[5] = String(readedData[31]) + String(readedData[32]);
.
.
.
EEPROM.write(EEPROM_MAC0, GivenMacAddress[0]);
              EEPROM.write(EEPROM_MAC1, GivenMacAddress[1]);
              EEPROM.write(EEPROM_MAC2, GivenMacAddress[2]);
              EEPROM.write(EEPROM_MAC3, GivenMacAddress[3]);
              EEPROM.write(EEPROM_MAC4, GivenMacAddress[4]);
              EEPROM.write(EEPROM_MAC5, GivenMacAddress[5]);
.
.
.
Ethernet.setMACAddress(GivenMacAddress);

The readedData is 30 AE A4 07 0D 64. How can i convert these values from string?

Why do you convert this values to String, if you need it in DEC?
If you do not convert them to a String, there will be no problem how to extract them back

How is readedData array defined? as chars, bytes, ints?
Where does this data come from?
Please show all your code.

Because i send it from a Delphi program, where I cannot convert the values properly. The readedData is a char array.

So what are the values :

Is 30 means two chars '3' and '0' or one char with code 30?

Why do you need to convert it to DEC?
What for?

It means two chars. The first char is "3" the second is "0". I want to convert it to DEC, because i cannot set the MAC address[0] with two Strings.

Look this thread. It contains ready-to-go code for proper conversion:

you can parse a char array using sscanf() converting each hex value to an integer, e.g.

void setup() {
  Serial.begin(115200);
  char s[]="30 AE A4 07 0D 64";
  unsigned int mac[6]={0};
  sscanf(s,"%x%x%x%x%x%x",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]);
  for (int i=0;i<6;i++) 
    { Serial.print(" "); Serial.print(mac[i],HEX);}   
}

a run gives an int array with the MAC address

30 AE A4 7 D 64

Ethernet.setMACAddress() requires a byte array as a parameter so you would have copy the data

Yes, but i cannot convert the GivenMacAddress[0] to a char array[0].

You have char array already -

as I read the problem you receive from Delphi a char array called readedData which contains "30 AE A4 07 0D 64" which represents a MAC address
to set that MAC address you have to convert the readedData char array to a byte array which is a parameter for Ethernet.setMACAddress()

Exactly, but i cannot convert it.

Your data is in char array already - in readedData[] . You don't need to convert it to String. Throw out your code from the post 1 .
Just put your readedData[] array in the code in post #7 instead of s[] array

The data comes in like:
readedData[21] = C
readedData[22] = 3 etc..
So the MAC address numbers are splitted.

have a look at

#include <SPI.h>
#include <Ethernet.h>

void setup() {
  Serial.begin(115200);
  char readedData[] = "30 AE A4 07 0D 64";
  unsigned int imac[6] = {0};
  // parse char array to get MAC values into int array
  sscanf(readedData, "%x%x%x%x%x%x", &imac[0], &imac[1], &imac[2], &imac[3], &imac[4], &imac[5]);
  // priint MAC values and copy to byte array
  byte mac[6];
  for (int i = 0; i < 6; i++)  {
    Serial.print(" ");
    Serial.print(mac[i] = imac[i], HEX);
  }
  Ethernet.setMACAddress(mac);    // set MAC address
  Serial.println("\nMAC address set");
}

void loop() {}

a run gives

30 AE A4 7 D 64
MAC address set

or is readData not "30 AE A4 07 0D 64" but also contains other information?
if so give an example

Yes the readedData contains other information. Like messageID .. I read the other information from 0-20, and from 21 the MAC address comes. Example code:

if (arrayIndex == 11) {
      msgVersion = (unsigned long)readedData[8] << 24;
      msgVersion |= (unsigned long)readedData[9] << 16;
      msgVersion |= (unsigned long)readedData[10] << 8;
      msgVersion |= (unsigned long)readedData[11];

      char recVersion[] = { "Recieved the version." };
      ws.send(WebSocket::DataType::TEXT, recVersion, strlen(recVersion));
    } 

in that case you parse from readedData[21], e.g.

  char readedData[] = "01234567890123456789 30 AE A4 07 0D 64";
  unsigned int imac[6] = {0};
  // parse char array to get MAC values into int array
  sscanf(&readedData[21], "%x%x%x%x%x%x", &imac[0], &imac[1], &imac[2], &imac[3], &imac[4], &imac[5]);

when run gives

30 AE A4 7 D 64
MAC address set

1 Like

So i did that:

sscanf(readedData[21] + readedData[22], "%x%x%x%x%x%x", &imac[0]);
            sscanf(readedData[23] + readedData[24], "%x%x%x%x%x%x", &imac[1]);
            sscanf(readedData[25] + readedData[26], "%x%x%x%x%x%x", &imac[2]);
            sscanf(readedData[27] + readedData[28], "%x%x%x%x%x%x", &imac[3]);
            sscanf(readedData[29] + readedData[30], "%x%x%x%x%x%x", &imac[4]);
            sscanf(readedData[31] + readedData[32], "%x%x%x%x%x%x", &imac[5]);
for (int i = 0; i < 6; i++) {
              Serial.println(imac[i], HEX);
            }

but the output was:

4:10:19.175 -> MAC: 
4:13:45.173 -> 1382
14:13:45.173 -> 2
14:13:45.173 -> 2
14:13:45.173 -> 441F
14:13:45.173 -> F00
14:13:45.173 -> 1035

The readedData[21] is the first letter or digit of the MAC address. The readedData[22] is the second.

No, it is wrong.
You were given a ready-made code....
Do it EXACTLY as in example post #16 if you don't understand how to work with arrays...

Yes but the ready-made code is wrong for me. It gives this output:
15:48:57.759 -> D64
15:48:57.759 -> 0
15:48:57.759 -> 0
15:48:57.759 -> 0
15:48:57.759 -> 0
15:48:57.759 -> 0
When i print it out like this:
for (int i = 0; i < 6; i++) {
Serial.println(imac[i], HEX);
}
I did exactly as above.

show your code

Add this lines before or after sscanf() function and show the output

for (int i = 21; i < 21+12; i++) {
Serial.println( readedData[i]);
}