Convert String to int

Hello, I am very new to Arduino programming, so I would like to know how to convert a received String from my Serial port to an int value.

For example, I receive two chars and buffer them in a char array.

  char Str[2] = {softSerial.read(), softSerial.read()};

so if str[2]={‘1’,‘2’};

Then I need to have a value which is

int value= 12;

How can I do it using Arduino programming language?

This should work.

char myStr[3] = {'1','2',0};
int myInt = atoi(myStr);

I receive two chars and buffer them in a char array.

Make sure that there are two things in the buffer to receive before you do this:-

if(Serial.available() >2) {  
Str[0] = softSerial.read(); 
Str[1] = softSerial.read();
}

SurferTim: This should work.

char myStr[3] = {'1','2',0};
int myInt = atoi(myStr);

Thanks a lot for your Help :)!

Grumpy_Mike:

I receive two chars and buffer them in a char array.

Make sure that there are two things in the buffer to receive before you do this:-

if(Serial.available() >2) {  
Str[0] = softSerial.read(); 
Str[1] = softSerial.read();
}

Yes I do :), I already receive a Char before each 2 chars, so I'm sure that the buffer has minimum of 2 chars.

Thanks a lot :)

But, if my char array is

      char Str3[2] = {'0','1','2'};

And i need to have the casted numeric value = 012;

How can i do it? I know I am thinking in a java way :D , but it's only because I am a java developer :(

And i need to have the casted numeric value = 012;

Octal conversion is tricky.

What is it you're trying to do?

I am using a joystick controller that sends over a zigbee Terminal from a java application. So I am controlling a servo degree in a vehicle, so this degree is 65=<degree<=115, And I am buffering for 3 chars as if i received 2 digits Integer it would be 065 or if i received 3 digits Integer it would be 115. Then I directly write this value to the servo. I know I can do a condition to check for the first received digit, if it’s zero then i would use the only last two values, But I am caring about complexity that’s it. Any Ideas :smiley: ?

I use sprintf() to convert data types to formatted strings. If you want a specific number of leading zeros, try this. It insures there are three characters, and if not, it adds the correct number of leading zeros.

int myInt = 12;
char outBuf[16];
sprintf(outBuf,"%03u",myInt);
Serial.println(outBuf);

For transmission, a leading space takes up exactly the same amount of space. For use on the receiver, the leading space is much easier to deal with than a leading 0, because a leading 0 is how functions that convert strings to numeric values know that you want the number interpreted as an octal value.

It is unlikely that you will be sending values less than 100 as octal, while sending values greater than or equal 100 as decimal.

Unless you are really weird, that is.

PaulS: (snip) It is unlikely that you will be sending values less than 100 as octal, while sending values greater than or equal 100 as decimal.

Unless you are really weird, that is.

If the programming language on the other end requires that format, then I'm really weird! I'll take that as a compliment. Thanks! XD

PaulS: For transmission, a leading space takes up exactly the same amount of space. For use on the receiver, the leading space is much easier to deal with than a leading 0, because a leading 0 is how functions that convert strings to numeric values know that you want the number interpreted as an octal value.

It is unlikely that you will be sending values less than 100 as octal, while sending values greater than or equal 100 as decimal.

Unless you are really weird, that is.

HAHA I understood you :D, And yes I agree with you :)

SurferTim: I use sprintf() to convert data types to formatted strings. If you want a specific number of leading zeros, try this. It insures there are three characters, and if not, it adds the correct number of leading zeros.

int myInt = 12;
char outBuf[16];
sprintf(outBuf,"%03u",myInt);
Serial.println(outBuf);

Thanks a lot for your help! :D that really helped me :)

SurferTim:

PaulS: (snip) It is unlikely that you will be sending values less than 100 as octal, while sending values greater than or equal 100 as decimal.

Unless you are really weird, that is.

If the programming language on the other end requires that format, then I'm really weird! I'll take that as a compliment. Thanks! XD

In this case only yes!, and that's why I used your code :D. I guess he meant only if the end parties are of the same language :D