Need help working with arrays and assigning values to long

So the issue I am having right now is the user inputs certain number, all of which are saved to an array. I know the specific format that needs to be used. So my problem is, how would i take the array values of index value 0 to 2 ... meaning the first 3 values typed and concatenate them into a variable of type long?

void loop(){
  
  //Reading Serial Values
  if( Serial.available())
  {
    char ch = Serial.read();
    
    if( ch == '\r')  // is this the terminating carriage return
    {
      buffer[ bufferIndex ] = 0; // terminate the string with a 0
      bufferIndex = 0;  // reset the index ready for another string
      Serial.print("Values = ");
      Serial.println(buffer);
      Serial.print("pH value = " ); 
      Serial.println(buffer[0]);
      pHValue = buffer[0];
      Serial.print("ORP value = ");
      Serial.println(buffer[2]);
      ORPValue = buffer[2];
      Serial.print("temperature = ");
      Serial.println(buffer[4]);
      temperature = buffer[4];
      pHtest();
    }
    else
      buffer[ bufferIndex++ ] = ch; // add the character into the buffer
  }
}

jz1031:
So the issue I am having right now is the user inputs certain number, all of which are saved to an array. I know the specific format that needs to be used. So my problem is, how would i take the array values of index value 0 to 2 ... meaning the first 3 values typed and concatenate them into a variable of type long?

Is it stored in the array as a character representation of the value, or is it stores as high/low bytes?

If the former, you can convert the ASCII equivalent to the numerical representation of a single character by simply subtracting '0' from the value. Example: '5' - '0' = 5. If you always know it's going to be exactly three numbers, then you know that a value of 245 is equal to 2 * 100 + 4 * 10 + 5 * 1. So plugging the three numbers into this formula would give you a numerical representation of the 3 digit number. Another way would be to copy the value into it's own little null terminated char array and use the atol() function.

For the former, you could use bit shifting and logical OR.

It is stored as a character representation of each number. Could you possibly post of an example code of how i'd combine those character representations into a double or long variable? Thanks.

jz1031:
It is stored as a character representation of each number. Could you possibly post of an example code of how i'd combine those character representations into a double or long variable? Thanks.

The formula is above. Just replace the immediate numbers with your array[index]- '0' and assign it to your long variable.

Arrch:

jz1031:
It is stored as a character representation of each number. Could you possibly post of an example code of how i'd combine those character representations into a double or long variable? Thanks.

The formula is above. Just replace the immediate numbers with your array[index]- '0' and assign it to your long variable.

Im not sure im being too clear. Say array[0] contained a 7 array[1] contains a period and array[2] contains an 8. So i want to assign 7.8 to the variable.

So i want to assign 7.8 to the variable.

But you said:-

the first 3 values typed and concatenate them into a variable of type long?

You can't put 7.8 into a long because a long is an integer and that number is not.

Grumpy_Mike:

So i want to assign 7.8 to the variable.

But you said:-

the first 3 values typed and concatenate them into a variable of type long?

You can't put 7.8 into a long because a long is an integer and that number is not.

ahh ok thank you, so how about float or double then? How would that be done?

jz1031:
ahh ok thank you, so how about float or double then? How would that be done?

Copy it into it's own string and run it through atof().

Simplest way is to make your character array big enough to hold one extra character. Store a null in that position. Then call the atof function to interpret the contents of the array as a float. Something like this:

char buf[4];
buf[0] = '7';
buf[1] = '.'";
buf[2] = '8';
buf[3] = '\0';
float f = atof(buf);

dc42:
Simplest way is to make your character array big enough to hold one extra character. Store a null in that position. Then call the atof function to interpret the contents of the array as a float. Something like this:

char buf[4];

buf[0] = '7';
buf[1] = '.'";
buf[2] = '8';
buf[3] = '\0';
float f = atof(buf);

Thank you very much! I applied this to my code and works perfectly :slight_smile: