I am making an app in which i have to encrypt the data and send it to server and in that case while converting data to int form i am getting number format exception .My code is as follows:
public String encryptData(String key, String s)
{
try{
System.out.println("encrypt");
// byte[] utf8 = data.getBytes("UTF8");
byte[] fin=s.getBytes();
System.out.println("encrypt2");
// byte[] enc = ecipher.doFinal(fin);
System.out.println("encrypt3");
}catch (Exception e) {
System.out.println(e);
}
System.out.println("1");
int keyLen = key.length();
// int dataLen = Convert.ToInt16(data.length());
System.out.println("2");
Integer dataLen=Integer.parseInt(s); // **This line is giving exception**
System.out.println("3");
char chData;
char chKey;
char[] data1 = s.toCharArray();
char[] key1 = key.toCharArray();
System.out.println("4");
StringBuilder encryptedData = new StringBuilder();
for (int i = 0; i < dataLen; i++)
{
chData = data1[i];
for (int j = 0; j < keyLen; j++)
{
chKey = key1[j];
chData = (char)(chData ^ chKey);
}
encryptedData.append(chData);
}
return (encryptedData.toString());
}
and My xml is :
xml="<?xml version='1.0' encoding='utf-8'?>" + "<admin_auth_req><user_name>" +username+ "</user_name>" + "<password>" +PWOrd+ "</password></admin_auth_req>";
Exception is:
java.lang.NumberFormatException: unable to parse '<?xml version='1.0' encoding='utf-8'?><admin_auth_req><user_name>newuser</user_name><password>tester</password></admin_auth_req>' as integer
`
You are triying to convert a
Stringvalue which is not a number representation intoString. What you really should do is to find the length of theString. So change this line:to: