I'm stuck at the very end of my problem getting a NumberFormatException.
I want to take a string such as Wsup, and turn it into its ASCII
values with the result, '87115117112'.
I've gotten that string of numbers built successfully a couple different ways, but when I try to parseInt(string)
on it, I get my exception. I've tried printing out the string as an array of characters to look for hidden white space, I've used String.trim()
with no luck, I'm quite confused why it isn't recognized as a valid integer.
public static int toAscii(String s){
StringBuilder sb = new StringBuilder();
String ascString = null;
long asciiInt;
for (int i = 0; i < s.length(); i++){
sb.append((int)s.charAt(i));
char c = s.charAt(i);
}
ascString = sb.toString();
asciiInt = Integer.parseInt(ascString); // Exception here
return 0;// asciiInt; 0 in place just to run
}
Thanks for any help given.
Yor asciiInt is long type so do it in this way
asciiInt = Long.parseLong(ascString);
here is your full function
public static long toAscii(String s){
StringBuilder sb = new StringBuilder();
String ascString = null;
long asciiInt;
for (int i = 0; i < s.length(); i++){
sb.append((int)s.charAt(i));
char c = s.charAt(i);
}
ascString = sb.toString();
asciiInt = Long.parseLong(ascString);
return asciiInt;
}