Splitting string to char array and byte array + hex to bytes

I have a string address included hex-values.

String address = "28 A8 FB 13 5 0 0 0 B0";

  1. Question: How can I split it to char array? And char-values to Byte-array?

char *arrayc;
or
char arrayc = new char[30];

array[0] = 28
array[1] = A8
array[2] = FB
etc.

  1. Question: How can I split it to byte array and change hex-values to bytes?

byte arrayb[8];

arrayb[0] = (HEX) 28;
arrayb[1] = (HEX) A8;
arrayb[2] = (HEX) FB;
etc.

The idea is to put all values to a byte array.

Time to be capital-S Sure about some things.

You are talking about char array strings and not C++ String objects? Just making sure!

tolerance_zero:
I have a string address included hex-values.

String address = "28 A8 FB 13 5 0 0 0 B0";

  1. Question: How can I split it to char array? And char-values to Byte-array?

char *arrayc;
or
char arrayc = new char[30];

array[0] = 28
array[1] = A8
array[2] = FB
etc.

  1. Question: How can I split it to byte array and change hex-values to bytes?

byte arrayb[8];

arrayb[0] = (HEX) 28;
arrayb[1] = (HEX) A8;
arrayb[2] = (HEX) FB;
etc.

The idea is to put all values to a byte array.

Please don't use C++ String! They're conveniences that keep you in the dark about your own code.

Learn C including pointers (address to data with data type info attached).

char myChars[] = "28 A8 FB 13 5 0 0 0 B0"; // makes a char array and initializes it.

char *chPtr; // a pointer to type char with no array, default initialized to point to nothing.

chPtr = myChars; // pointer chPtr now points to myChars data.

Note that the name myChars is a char pointer to the data in the array

The data in myChars is not hex values 2B A8 FB 13 5 0 0 0 B0, it is the ASCII text values for every letter, number and space in that string and there is another 0 added to the end, the array is 23 chars long.

char myChars[] = { "28 A8 FB 13 5 0 0 0 B0" };
byte buffer[ 8 ]; // defaults to all 0's

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

  Serial.println( );
  for ( byte i = 0; i < sizeof( myChars ); i++ )
  {
    Serial.print( F( "Char '" ));
    Serial.print( myChars[ i ] );
    Serial.print( F( "' converts to " ));
    Serial.print((byte)(myChars[ i ]), HEX );
    Serial.print( F( " Hex = " ));
    Serial.print((byte)(myChars[ i ]));
    Serial.println( F( " decimal" ));

  }
  Serial.println( );
}

void loop() {
  // put your main code here, to run repeatedly:

}

Hex 28 (0x28) == 2 x 16 + 8 == 40 decimal

You have to read the chars and convert to hex or use a function to save you from knowing what you're doing.

If people would learn the simple basics first, there would be better questions asked here.

Another way that should work using strtok() to parse the input string is:

char myChars[] = { "28 A8 FB 13 5 0 0 0 B0" };
char buffer[9][5]; // defaults to all 0's
byte values[9];

void setup()
{
  Serial.begin( 115200 );
  char *ptr;
  char temp[3];
  int count = 0;
  
  ptr = strtok(myChars, " ");
  while (*ptr) {                  // Parse into substrings
    strcpy(temp, ptr);            // Save the substring
    strcpy(buffer[count], "0x");  // Identify it as a hex string
    strcat(buffer[count], temp);  // Tack on hex value
    values[count] = (byte) strtoul(buffer[count], 0, 16);   // Convert to unsigned byte
    count++;
    ptr = strtok(NULL, " ");      // On to the next substring
  }

  for ( byte i = 0; i < 9; i++ )
  {
    Serial.println((byte)values[i]);  // Show the results
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

Edit: Actually, part of the code in the while loop is not needed since strtoul() makes the proper conversion:

  while (*ptr) {                  // Parse into substrings
    strcpy(buffer[count], ptr);            // Save the substring
    values[count] = (byte) strtoul(buffer[count], 0, 16);   // Convert to unsigned byte
    count++;
    ptr = strtok(NULL, " ");
  }

Thanks econjack. Your code works good.

how can i remove that error

how can i remove that error

With a cast.

   values[count] = (byte) strtoul((const char *)buffer[count], 0, 16);

same problem :frowning:

same problem

No, it isn't. Read the damned message. You can figure out the proper cast.

PaulS:
No, it isn't. Read the damned message. You can figure out the proper cast.

no one Damnad in that place ....????