Search code examples
c++arraysstringarduinoi2c

Concatenating chars from a char array into one integer


I've been trying to create serial motor controller from an ATtiny 85. I have it setup as a slave. It receives chars from a master one byte at a time and loads them into a char array for parsing.

The problem I'm having is I can't find a way to concatenate them instead of adding the values.

rxChrData = TinyWireS.receive();
rxString[rxIndex] = char(rxChrData);
rxIndex++;  
if (rxChrData == ':'){
          //rxString 2-3 each holds the value 9.
          //The lines below print "27" on the serial line.
          n = int(rxString[2]+rxString[3]+rxString[4]);
          Serial.println(n);
          rxIndex = 0;  
      }

The value of n is 27 and I need it to be 999.

I've tried converting it to a String, concatenate, then back to an integer. No luck. I've also tried a few other inept work arounds with no luck.


Solution

  • Like this

    n = 100*rxString[2]+10*rxString[3]+rxString[4];
    

    or maybe this

    n = rxString[2]+10*rxString[3]+100*rxString[4];