Convert string to int

Hello, i have a string variable that may have the value "+057" for example (or "-452). I need the plus and the minus symbols.

Do you know how to convert it to an int variable.

Thanks in advance

If you have a string (lower case s), use atoi().

If you have a String (upper case s), use the toInt() method.

Thanks

Assume, you have:

char myData[] = "+57";
==> char myData[] = {0x2B, 0x35, 0x37};

Serial.begin(9600);
  char myData[] = "+57";
  byte x1 = myData[0];
  byte x2 = (myData[1]<<4)|(myData[2]&0x0F);
  Serial.print((char)x1);
  Serial.print(x2, HEX); //+57

GolamMostafa:
char myData[] = "+57";
==> char myData[] = {0x2B, 0x35, 0x37};

You forgot the delimiting '\0'.

==> char myData[] = {0x2B, 0x35, 0x37, 0x00};

Whandall:
You forgot the delimiting '\0'.

==> char myData[] = {0x2B, 0x35, 0x37, 0x00};

I have seen coding elsewhere in this Forum where the null-byte ('\0' = 0x00) has not been explicitly included in the definition. Later on, I printed the member myData[4]; I found 0. From this evidences, I have started to believe that the null-byte is automatically added by the Compiler while it makes the binary codes.

From this evidences, I have started to believe that the null-byte is automatically added by the Compiler while it makes the binary codes.

It is. But, your claim that

char myData[] = "+57";

is equivalent to

char myData[] = {0x2B, 0x35, 0x37};

is wrong.

I have started to believe that the null-byte is automatically added by the Compiler while it makes the binary codes.

The compiler does obey the rules for creating C-strings, which are well documented.

PaulS:
It is. But, your claim that
char myData[] = "+57";
is equivalent to
char myData[] = {0x2B, 0x35, 0x37};
is wrong.

It (char myData[] = {0x2B, 0x35, 0x37}:wink: could be 'incorrect'; however, how it could be 'wrong' when it is responding in the same way to the print() command as this (char myData[] = "+57":wink: one? When I tell the ATmega328P MCU to add the BCD numbers: 9 and 1, it gives me the result A instead of 10; is it 'incorrect' or 'wrong'?

Also, given below, there is an example of a character array which has not explicitly contained the null-byte.

charArray.png

charArray.png

Also, given below, there is an example of a character array which has not contained the null-byte explicitly.

Yes, that is fine and perfectly legal.

But that array cannot be used with any of the C-string functions that expect the null byte terminator. For example, operations as simple as strlen() won't give the correct answer and Serial.print(name) will produce garbage.

jremington:
Yes, that is fine and perfectly legal.

But that array cannot be used with any of the C-string functions that expect the null byte terminator. For example, operations as simple as strlen() won't give the correct answer and Serial.print(name) will produce garbage.

Serial.begin(9600);
  char myData[] = "+57";  
  char myData1[] = {0x2B, 0x35, 0x37};  
  char myData2[] = {'+', '5', '7'};   
  char myData3[] = {'+', '5', '7', '\0'}; 
  Serial.println(myData);  //prints: +57
  Serial.println(myData1);  //prints: +57
  Serial.println(myData2);  //prints: garbage (+57+57)
  Serial.println(myData3);  //prints: +57

Now, getting some knowledge from your post that a user is required to be familiar with the 'set rules' of the C/C++ Language as to the valid declaration/definition of a character array.

However, from the print out of the aboe codes, it appears that 'char myData[] = "+57";' and 'char myData1[] = {0x2B, 0x35, 0x37};' are equivalent -- an assertion of this poster is being refuted in Post#6.

Also, there is a new query in my mind why these two arrays are not equivalent: 'char myData1[] ={0x2B, 0x35, 0x37};' and 'char myData2[] = {'+', '5', '7'};'?

Please stop posting such nonsense. You are confusing the beginners as well as yourself.

That this line

char myData1[] = {0x2B, 0x35, 0x37};

seems to print correctly in that one particular case is simply an accident.

There can accidentally be a zero, or nonprinting characters following the data statement in memory, and the serial monitor won't show that.

it appears that 'char myData[] = "+57";' and 'char myData1[] = {0x2B, 0x35, 0x37};' are equivalent

Not so, as you can easily demonstrate to yourself by avoiding such simple traps.

But I can help! Try this and explain to us the result:

// data are global
char myData[] = "+57";
char myData1[] = {0x2B, 0x35, 0x37};
char myData2[] = {'+', '5', '7'};
char myData3[] = {'+', '5', '7', '\0'};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println(myData);  
  Serial.println(myData1); 
  Serial.println(myData2);
  Serial.println(myData3); 
}

void loop() {
  // put your main code here, to run repeatedly:

}

jremington:
Please stop posting such nonsense.

This is a vocabulary for an English speaking native speaker as I have understood being in this Forum for more than a year; but, it is an insult/slang to the non-native.

What does my version of your program print, and how do you explain the result?

jremington:
What does my version of your program print, and how do you explain the result?

Your version of my program is repeated below with output result:

// data are global
char myData[] = "+57";
char myData1[] = {0x2B, 0x35, 0x37};
char myData2[] = {'+', '5', '7'};
char myData3[] = {'+', '5', '7', '\0'};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println(myData);  //prints: +57
  Serial.println(myData1); //prints garbage : +57+57
  Serial.println(myData2); //prints garbage: +57+57+57
  Serial.println(myData3); //print: +57
}

void loop() {
  // put your main code here, to run repeatedly:

}

My Remarks:
(1) I went back to 5/12/1995 to review my MSDOS Programming exercise to see what I did to print a string of characters to the display -- I had to include '$' (the terminating character) at the end of the string as a rule of DOS/INT21 Programming in order to allow the program to detect the end-of-string.

(2) Now, I have learnt that the variable declaration should remain valid regardless of its placement either in the local space or in the global space. So, the valid declaration for our concerned string is that it must have an explicit null-byte at the end except this style 'char myData[] = "+57";' in which the compiler automatically adds null-byte

(3) At the moment, I cannot conceive any other idea to interpret the significance of placing the variables in the global space. May be, my pupils could have better ideas which are still to arrive by about a month. In the meantime, if someone comes up with knowledge that will be a bonus.

Chrypapa:
Hello, i have a string variable that may have the value "+057" for example (or "-452). I need the plus and the minus symbols.

Do you know how to convert it to an int variable.

Thanks in advance

the Integer.parseInt(s.trim()) method is used to change from the string s to the integer i in this line of code:

1
int i = Integer.parseInt(s.trim());

AjayDubedi:
the Integer.parseInt(s.trim()) method is used to change from the string s to the integer i in this line of code:

1
int i = Integer.parseInt(s.trim());

Your Arduino runs Java?

PaulS:
If you have a string (lower case s), use atoi().

If you have a String (upper case s), use the toInt() method.

if The String includes a non numeric character, it returns 0, for websites input conversion i ended up doing something else ("0" is a valid input and i didn't want non-valid input to be treated as such.)