0
votes

I' am trying to convert string to int(like Integer.parseInt() in java) in arduino in order to make some operation's on the numbers. Unfortunately none of my solution's worked. Until now I tried:

Create char Array and call atoi function:

String StringPassword;

uint8_t *hash;
//Here I define hash

int j;
for (j = 0; j < 20; ++j) {
 StringPassword.concat(hash[j]);
}

 //Checking String Size
 Serial.println("Size");
 //Checking String
 Serial.println(StringPassword.length());
Serial.println(StringPassword);

int jj;
char PasswordCharArray[StringPassword.length()];
 StringPassword.toCharArray(PasswordCharArray, StringPassword.length());

awa = atoi(PasswordCharArray);  
Serial.println(awa);

Output:

Size 48

168179819314217391617011617743249832108225513297

18209

Create char Array for null terminated string and call atoi function:

 String StringPassword;

 uint8_t *hash;
 //Here I define hash

int j;
for (j = 0; j < 20; ++j) {
 StringPassword.concat(hash[j]);
}

 //Checking String Size
 Serial.println("Size");
 //Checking String
 Serial.println(StringPassword.length());
Serial.println(StringPassword);

int jj;
char PasswordCharArray[StringPassword.length()+1];
 StringPassword.toCharArray(PasswordCharArray,StringPassword.length()+1);

awa = atoi(PasswordCharArray);  
Serial.println(awa);

Output:

Size

48

168179819314217391617011617743249832108225513297

-14511

use toInt Function:

 String StringPassword;

  uint8_t *hash;
 //Here I define hash

int j;
 for (j = 0; j < 20; ++j) {
   StringPassword.concat(hash[j]);
 }

 //Checking String Size
 Serial.println("Size");
 //Checking String
  Serial.println(StringPassword.length());
Serial.println(StringPassword);

awa = StringPassword.toInt();
Serial.println(awa);

Output:

Size

48

168179819314217391617011617743249832108225513297

-14511

What is the proper way of changing String to Int so:

awa = 168179819314217391617011617743249832108225513297 ?

And could someone explain to me why my solution's didn't worked? I tried to use the function's that were mentioned on Stackoverflow and Arduino forum to solve this.

1
Java can't convert such big number either.gre_gor

1 Answers

0
votes

The number 168179819314217391617011617743249832108225513297 reaches the maximum integer value limit so therefore this will not convert into an integer.

Try using atol() instead of atoi(). Long numbers can hold more data like the number shown above.