Reputation: 31
I am trying to take in input from users as a string, divide this string into two halfs and convert those strings to hexadecimal ints. I need to use these hexadecimal ints in a TEA encryption algorithm. I am making this code in netbeans for the gui builder.
My Current Code
//Getting initial text
String arg = input.getText();
//establishing Left and Right for TEA
int[] v = new int[2];
// Splitting the string into two.
StringBuilder output2;
for (int x = 0; x < 2; x++ ) {
output2 = new StringBuilder();
for (int i = 0; i < arg.length()/2; i++) {
if (x == 1)
output2.append(String.valueOf(arg.charAt(arg.length()/2 + i)));
else
output2.append(String.valueOf(arg.charAt(i)));
}
//converting a half into a string
String test = output2.toString();
//printing the string out for accuracy
System.out.println(test);
//converting the string to string hex
test = toHex(test);
//converting the string hex to int hex.
v[x] = Integer.parseInt(test, 16);
}
public static String toHex(String arg) {
return String.format("%x", new BigInteger(arg.getBytes()));
}
I get this error:
java.lang.NumberFormatException: For input string: "6a54657874"
Ive looked around the web for this issue but the error says it is happening when im converting the string to v[x] which multiple sites say is the correct way to put a hex string into an int so i am confused. Please help.
Upvotes: 0
Views: 232
Reputation: 5883
32 bits is too small for your number. You'll need to use Long.parseLong
instead.
Upvotes: 1
Reputation: 37813
6a54657874
as hex is 456682469492
in decimal. This is bigger than Integer.MAX_VALUE
. It will fit into a long
.
Make v
a long[]
and use Long.parseLong(test, 16);
Upvotes: 2