Specific character place in string to single int

**Solved. Was using "0" and not '0'. **
Thank you Bob.

I'm working on a way to manipulate a 2 dimensions array from serial command on an arduino. Problem is I'm able to take the string, seperate certain characters to multuple variables, but I can't seem to make these variables integers where I can make use of them injecting them into their necessary position of the array. Then seem to not convert from accii? to integers.

int Bit8;
int Bit9;
int Bit10;
int Bit12;
int Bit13;
int Bit14;
int Bit15;
int Bit16;

String command;

Disregard the following voids as it's irrelevant to the issue. 
void Setup()
void exportArray() {

void loop() {
    if (Serial.available()) {
    command = Serial.readStringUntil('\n');
    command.trim();
      if (command.equals("Print Array")) {
        Serial.println("Exporting Array");
        exportArray();
      } else if (command.equals("Help")) {
        Serial.println("Command Options");
        Serial.println("Commands:");
        Serial.println("Print Array");
        Serial.println("Change *** *****");
      } else if (command.startsWith("Change")){
        Serial.println("Changing");
        Bit8 = command[7];
        Bit9 = command[8];
        Bit10 = command[9];
        Bit12 = command[11];
        Bit13 = command[12];
        Bit14 = command[13];
        Bit15 = command[14];
        Bit16 = command[15];
//        Bit8 = atoi(Bit8);
//        Bit9 = atoi(Bit9);
//        Bit10 = atoi(Bit10);
//        Bit12 = atoi(Bit12);
//        Bit13 = atoi(Bit13);
//        Bit14 = atoi(Bit14);
//        Bit15 = atoi(Bit15);
//        Bit16 = atoi(Bit16);
        Serial.print(Bit8);
        Serial.print(Bit9);
        Serial.print(Bit10);
        Serial.print(" ");
        Serial.print(Bit12);
        Serial.print(Bit13);
        Serial.print(Bit14);
        Serial.print(Bit15);
        Serial.println(Bit16);
      } else {    
        Serial.println("bad command");
      }
    Serial.print("Command: ");
    Serial.println(command);
    }
  }

If I enter Change 123 12345 into the serial monitor this is what gets returned.
16:45:20.088 -> Changing
16:45:20.088 -> 505132 505152530
16:45:20.088 -> Command: Change 123 12345

If I enable the Bit* = atoi(Bit*); in the code this is what's returned.
16:47:16.211 -> Changing
16:47:16.211 -> 000 00000
16:47:16.211 -> Command: Change 123 12345

I've dug through other ideas and saw one instance where someone was able to do...
Bit8 = ((Bit8) - "0");
But I endup with "invalid operands of types 'int' and 'const char [2]' to binary 'operator-'" when trying to do a compile.

What am I missing or doing wrong here.

Disregard the void Setup() as it's irrelevant.

Is the exportArray() function irrelevant as well ?

How do you know what is irrelevant ?

Yes, it is. It works. I cannot paste the whole code as it exceeds the post limit.

I also just realized I was counting from 1 on and not 0 on in the command string. I'm now getting the correct ascii characters out but I still need to convert them into integer.

17:29:07.045 -> Changing
17:29:07.045 -> 495051 5253545556
17:29:07.045 -> Command: Change 123 45678

I'll update the code above.

I've dug through other ideas and saw one instance where someone was able to do...
Bit8 = ((Bit8) - "0");
But I endup with "invalid operands of types 'int' and 'const char [2]' to binary 'operator-'" when trying to do a compile.

Each character of a String is actually a single char. To convert that char digit to an integer subtract the ASCII value of zero (or simply '0') from it

An example

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  String n1 = "12345";
  Serial.println(n1[0]);
  byte n2 = n1[0];
  Serial.println(n2);
  byte n3 = n2 - '0';  //note single quotes
  Serial.println(n3);
  Serial.println(n3 * 99);  //prove that n3 is actually an integer
}

void loop()
{
}

UKHeliBob:
Each character of a String is actually a single char. To convert that char digit to an integer subtract the ASCII value of zero (or simply '0') from it

An example

void setup()

{
  Serial.begin(115200);
  while (!Serial);
  String n1 = "12345";
  Serial.println(n1[0]);
  byte n2 = n1[0];
  Serial.println(n2);
  byte n3 = n2 - '0';  //note single quotes
  Serial.println(n3);
  Serial.println(n3 * 99);  //prove that n3 is actually an integer
}

void loop()
{
}

Yes, I tried to implement that into the code but get the error message shown at the end of the first post when defining Bit8 to Bit9 in the beginning as either int or byte. Kinda lost.

Are you confusing "0" and '0' ?

They are not the same thing

UKHeliBob:
Are you confusing "0" and '0' ?

They are not the same thing

Hah! That was my problem. Lizard brain sometimes.
Thank you Bob!!

Sigh, only being able to post every 5 minutes is a pain in butt.

Glad you got there

By the way, atoi() expects a C style string (an array of chars terminated by a zero) as its parameter. That's why you can't use it to convert a single char to an integer

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