# Arduino serial.read to int

I am trying to read a serial string which comes through as "R0123" for example then I need the 0123 to be in an int. to send out through another method

Here is my code it is not working the way that I think it should

void loop()
{
if(Serial.available())
{
delay(100);
{
int r1 = Serial.read();
int r2 = Serial.read();
int r3 = Serial.read();
int r4 = Serial.read();
int red = ((int)r1 * 1000) + ((int)r2 * 100) + ((int)r3 * 10) + (int)r4;

sb.sendColour(red,0,0);

Serial.print(r1,0);
Serial.print(r2,0);
Serial.print(r3,0);
Serial.print(r4,0);
Serial.print(red);
}
}
}




-

What about using atoi()? You will still need to trim of the 'R', and you might have to pad it with a NULL character at the end ('\0'). But then it should work as:

int rInteger = atoi(s);


when s is a pointer to a null terminated string.

-
So if i do Serial.read into a string for the four characters then add \o and run in it atoi it should work – Ashley Hughes Aug 26 '12 at 9:55
I am getting an error ArduinoShiftyWireless.cpp: In function 'void loop()': ArduinoShiftyWireless:45: error: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)' – Ashley Hughes Aug 26 '12 at 10:04
Like I said, atoi() doesn't take a string, it takes a pointer to a string, or to an array of chars, hence the error :) I am not sure if arduino uses ANSI C(?) Never the less, this should be accurate enough: acm.uiuc.edu/webmonkeys/book/c_guide/2.13.html#atoi – Bladt Aug 26 '12 at 10:13
Frankly, I'm a little rusty on my C, bit something like //code to read the 'R' character char* readString; for(int i=0; i<4; i++) { readString[i]=Serial.Read(); } int a = atoi(readString); I imagine would do the trick. – Bladt Aug 26 '12 at 10:43
Ehh formatting? See it here: pastebin.com/Q4GCLh75 – Bladt Aug 26 '12 at 10:49

You are confusing integer values and ascii character values. When your

  int r1 = Serial.read();


reads a 0 it will read the ASCII character 0. The integer value of the character 0 is (decimal) 48 (check for instance this ascii table). So instead that line should read

  int r1 = Serial.read() - 48;


or even better

  int r1 = Serial.read() - '0';

-

This reads 4 characters after the "R", places them in a char array and appends a null character. Then converts to integer. The code doesn't check if there are at least 4 chars following the "R", or that they are digits, though.

#define numberOfDigits 4
char theNumberString[numberOfDigits + 1];
int theNumber;

{
for (int i = 0; i < numberOfDigits; theNumberString[i++] = Serial.Read());
theNumberString[numberOfDigits] = 0x00;
theNumber = atoi(theNumberString);
}

-

You can try:

int intParse(byte length) //return parsed byte from serial
{
char BUFFER[length + 1];
delay(5); //need for Baud 9600, less delay for faster speed
for(byte i = 0; i < length; i++)
BUFFER[length] = 0; //I had this earlier, but not necessary for Arduino.
return atoi(BUFFER);
}


For example: R1234 You would write:

void loop()
{
char input = Serial.read();
if(input == 'R')
{
int output = intParse(4);
}
}

-
You should also set BUFFER[length] = 0;, as C doesn't guarantee this by default. – Ben Voigt May 2 '13 at 1:10

more simple code using parseInt()

void loop()
{
if (Serial.available())
{
int f = Serial.parseInt();
if (f > 0)
{
Serial.println(f);
}
}
}

-

Why not just use the "readString()" function? Then you can have your code as:

void setup() {
Serial.begin(9600);
Serial.println("Waiting for intstructions...\n");
}

void loop() {
if(Serial.available()) {
`