String to Array, how to solve!

const String hexDigits = "0123456789ABCDEF";

byte* msgVehiclePosition[8] = {0, 0, 0, 0, 0, 0, 0, 0};

String data0 = "D1";
String data1 = "9A";
String data2 = "D7";
String data3 = "8B";
String data4 = "18";
String data5 = "19";
String data6 = "2F";
String data7 = "9D";

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

  msgVehiclePosition[0] = data0;
  msgVehiclePosition[1] = data1;
  msgVehiclePosition[2] = data2;
  msgVehiclePosition[3] = data3;
  msgVehiclePosition[4] = data4;
  msgVehiclePosition[5] = data5;
  msgVehiclePosition[6] = data6;
  msgVehiclePosition[7] = data7;

  Serial.println(msgVehiclePosition);
}

void loop() {
}

//Output = msgVehiclePosition = {D1, 9A, D7, 8B, 18, 19, 2F, 9D}
//Output = msgVehiclePosition = {209, 154, 215, 139, 24, 25, 47, 157}

Avoid using Strings, they are never necessary and on AVR-based Arduinos, cause memory problems and program crashes.

Do you mean something like this?

//Output = msgVehiclePosition = {D1, 9A, D7, 8B, 18, 19, 2F, 9D}
char msgVehiclePosition[8]={0xD1, 0x9A, ...

To use Serial.print() with that (as printable characters), you need to define a C-string, which is that array, declared to be of length 9 rather than 8, with a terminating zero byte.

To print the individual array values as decimal numbers

for (int i=0; i<8; i++) Serial.println((unsigned int) msgVehiclePosition[i]);

You are defining a two-byte string

and then you try to assign this two bytes into a one-byte variable

This can not work.

before anything else post a description in normal words what you want to do.

Avoid any kind of code in your description. With the above code you have shown that you have some misconceptions about variables. And this is the reason why your description shall not include new misconceptions.

Use the word "letter" and the word "value" in your description.

best regards Stefan

@StefanL38 you are correct except for a different reason. msgVehiclePosition is actually an array of pointers (to bytes).

"String to Array, how to solve!" does not adequately describe what your problem is since there are multiple ways to interpret that. Do you want those string values in an array? Do you want the binary hex values of the converted strings?

Please clarify.

Judging from this comment, you probably want to initialise the array as follows.

byte msgVehiclePosition[] {
  0xD1, 0x9A, 0xD7, 0x8B, 0x18, 0x19, 0x2F, 0x9D};

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.