تبدیل string به int با الگوریتم خودم.
public static int strToint(String e) throws NumberFormatException {
if (e == null || e.length() < 1 || e.equals("")) //string moshkel dare
throw new NumberFormatException("String : " + e + " is corrupt !");
char[] ch = e.toCharArray();
boolean negative = false; //manfi
int len = ch.length; // toole adad
int[] i = new int[len]; //araye adad
short j, k; // adad halghe ha
int result = 0; //javab
if (ch[0] == '-') { //manfi
if (e.length() == 1)
throw new NumberFormatException("String : " + e + " is corrupt !");
negative = true; // manfi mishe
ch = e.substring(1).toCharArray();
len = ch.length;
} else if (ch[0] == '+') { //mosbat
if (e.length() == 1)
throw new NumberFormatException("String : " + e + " is corrupt !");
ch = e.substring(1).toCharArray();
len = ch.length;
}
for (j = 0; j < len; j++) {
if (ch[j] < '0' || ch[j] > '9') // bayad beine 0 ta 9 bashe
throw new NumberFormatException("String : " + e + " Not a Number !");
i[j] = ch[j] - 48; // az char ke adad bashe 48 ta kam koni mishe int
for (k = 0; k < len - (j + 1); k++)
i[j] *= 10; //sefr ha ra mizaram ba zarb dar 10
result += i[j]; // hala jam mikonam
if (result < 0) //aslan nabayad manfi bashe
{
if (result == -2147483648 && negative) {//No problem (-|0)
// irad az man nist . system manfi bar migardoone . baray hamin in shart lazeme
} else //adad bozorg tar az int
{
throw new NumberFormatException("Number : " + e + " is out of range for int! ");
}
}
}
return negative ? -result : result; // manfi bashe ya mosbat
}