How do you convert char 4 to int 4

Hello,

I'd like to convert the following

char charZoneStatus;

to

int intZoneStatus;

charZoneStatus will only ever hold a 1, 2, 3, or 4.

I've found a few threads on this topic but I haven't been able to understand them.

Thanks :slight_smile:

A char variable can hold the value 4. It can also hold the value '4'. They are NOT the same value.

char cFour = '4';
char cZero = '0';

int iFour = cFour - cZero; // iFour will be 4

Thanks! I tested the code you shared and it works well.

I also tried removing the ' 's from the numbers like this:

char cFour = 4;
char cZero = 0;
int iFour;

void setup() {
  Serial.begin(9600);
}

void loop() {
  iFour = cFour - cZero; // iFour will be 4
  Serial.println(iFour);
  delay(2000);
  if (iFour == 4) {
    iFour = 1;
  Serial.println(iFour);
  }
  delay(2000);
}

And it also worked.

But I still seem to not understand something, because I can't translated it to my code successfully. Can you tell me why my if statement isn't working? Look for NOT WORKING* comment.

// Example 3 - Receive with start- and end-markers

const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;
char zoneStatus;

void setup() {
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
}

void loop() {
  recvWithStartEndMarkers();
  showNewData();
}

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }
    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}

void showNewData() {
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    zoneStatus = receivedChars[0];
    Serial.print("zoneStatus is: ");
    Serial.println(zoneStatus);
    if (zoneStatus == 3) {                 // <-------------------- ****NOT WORKING**** ----------------------
      Serial.print("If statement worked, and zoneStatus is: ");
      Serial.println(zoneStatus);
    }
    newData = false;
  }
}

I presume this relates to your other Thread

When writing Reply #17 t had crossed my mind that you might want the value as an int but I had thought I might be over-complicating matters.

Rather than copy the character from receivedChars and then convert it to an int you can do it in one step with

intZoneStatus = atoi(receivedChars);

The atoi() function converts a string of Ascii chars to an integer.

By the way, it makes things much easier if you ask all the questions about one project in the same Thread where everyone can easily see all the relevant info.

...R

SheCurvesMobius:
Look for NOT WORKING* comment.

    if (zoneStatus == 3) {                 // <-------------------- ****NOT WORKING**** ----------------------

}

You are mixing up numbers with Ascii characters. When you send '3' with the Serial Monitor you actually send the value 51 which is the Ascii code for the character '3'.

So your code is comparing 51 with 3.

If the numbers are not important the simple solution might be

if (zoneStatus == '3') {

By putting the 3 into single quotes you are telling the compiler to use the Ascii value for the character.

...R

Whoops, my mistake. I should have kept it in the same thread. I thought that it would be better to separate it because it was a question that deviated enough from the initial question. I'll keep questions in the same thread in future.

Thanks again for all of your help Robin2!