1

I have this part of my code:

int[] myIntArray = {0x2e80,0x008c,0x0993,0x09c5,0x058b,0x4c9c,0x0390,0x1e96,0x0989,0x0ac4,0x4cad,0x0d93,0x09c5,0x0a84,0x0591,0x04c5,0x058b,0x4c9c,0x0390,0x1ec5,0x0d87,0x0589,0x0591,0x0580,0x1fc4,0x4cb2,0x0591,0x048a,0x1991,0x4c84,0x4c8d,0x1988,0x0e89,0x09c5,0x0e90,0x18c5,0x1e80,0x0d96,0x038b,0x0d87,0x0080,0x4c86,0x038b,0x0a8c,0x0880,0x0286,0x09c5,0x058b,0x4c9c,0x0390,0x1ec5,0x0392,0x02c5,0x1c8a,0x1b80,0x1e96,0x4c9c,0x0390,0x4c86,0x0d8b,0x028a,0x18c5,0x0e80,0x4c96,0x1986,0x0f80,0x1f96,0x0a90,0x00c5,0x0397,0x4c8d,0x0d95,0x1c9c,0x42e5};
for (int i=0; i<=73; i++){
  String s1=Decrypt(k,myIntArray[i]);
  String s2= s1.substring(2,6);
  String s=convertHexToString(s2);
  System.out.print(s);
}

That takes hex values from the array and do some operations on it. And its working just fine as i want.

I want to do the same thing but i want to read the values from a file and do the same operations on it, i tried this :

String token1 = "";
Scanner inFile1 = new Scanner(new    File("chipertext.txt")).useDelimiter(",\\s*");
List<String> temps = new ArrayList<String>();
while (inFile1.hasNext()) {
  token1 = inFile1.next();
  temps.add(token1);
}
inFile1.close();
String[] tempsArray = temps.toArray(new String[74]);
int[] myIntArray = new int[tempsArray.length];

for (int i = 0; i < tempsArray.length; i++) {
  myIntArray[i] = Integer.parseInt(tempsArray[i]);
}
for (int i=0; i<=73; i++){
  String s1=Decrypt(k,myIntArray[i]);
  String s2= s1.substring(2,6);
  String s=convertHexToString(s2);
  System.out.print(s);
}

But i get this error :

Exception in thread "main" java.lang.NumberFormatException: For input string: "0x2e80  0x2e80
0x008c
0x0993
0x09c5
0x058b
0x4c9c
0x0390
0x1e96
0x0989
0x0ac4
0x4cad
0x0d93
0x09c5
0x0a84
0x0591
0x04c5
0x058b
0x4c9c
0x0390
0x1ec5
0x0d87
0x0589
0x0591
0x0580"

the values stored in the file like this :

0x2e80
0x008c
0x0993
0x09c5
0x058b
0x4c9c
0x0390
0x1e96
0x0989
0x0ac4
0x4cad
0x0d93
0x09c5
0x0a84
0x0591
0x04c5
0x058b
0x4c9c
0x0390
0x1ec5
0x0d87
0x0589
0x0591
0x0580

I think this means that that string cant be stored as integer right? then how to do it ? and how it was stored in integer array before ?! i don't know can someone please help me?

WORKING CODE

String token1 = "";
Scanner inFile1 = new Scanner(new File("chipertext.txt"));
List<String> temps = new ArrayList<String>();
while (inFile1.hasNext()) {
token1 = inFile1.next();
temps.add(token1);
}
inFile1.close();
String[] tempsArray = temps.toArray(new String[73]);
int[] myIntArray = new int[tempsArray.length];
for (int i = 0; i < tempsArray.length; i++) {
myIntArray[i] = Integer.parseInt(tempsArray[i].substring(2), 16);
}
for (int i=0; i<=73; i++){
   String s1=Decrypt(k,myIntArray[i]);
   String s2= s1.substring(2,6);
   String s=convertHexToString(s2);
   System.out.print(s);
}

Thank you all for the help !!!

Lamawy
  • 603
  • 1
  • 7
  • 12
  • Please post how is the data stored within the file you are using. – npinti Apr 27 '15 at 13:19
  • It means somewhere you're attempting to read `0x2e80 .......` as input. Try some debugging. Check your input file and also do a Sysout in your loop of each token to see what you're storing. – CubeJockey Apr 27 '15 at 13:22
  • try this: myIntArray[i] = Integer.parseInt(tempsArray[i], 16); – VeryNiceArgumentException Apr 27 '15 at 13:25
  • @Trobbins i did try to print each token and its correct! – Lamawy Apr 27 '15 at 13:27
  • @RaphaelMoita tried it and same error :( – Lamawy Apr 27 '15 at 13:28
  • How are you generating the hexadecimal numbers? Is it `toHexString`? – npinti Apr 27 '15 at 13:37
  • Reading your code, you are supposed to separate your values with commas, plus, it would help if you posted the actual error and file used – jamp Apr 27 '15 at 13:37
  • @npinti no i have them already, i didnt use any function to generate them. – Lamawy Apr 27 '15 at 13:39
  • @jamp this is the actual file im using and the actual error message, but i didnt post them all just because they are long – Lamawy Apr 27 '15 at 13:40
  • is this part of the actual error message. For input string: "0x2e80 ......." ? – jamp Apr 27 '15 at 13:41
  • @jamp should i save them in the same line but seperated by comma's in the file ? like 0x0589, 0x0591, 0x0580 ... – Lamawy Apr 27 '15 at 13:41
  • @jamp replace the dots with these 0x2e80 0x008c 0x0993 0x09c5 0x058b 0x4c9c 0x0390 0x1e96 0x0989 0x0ac4 0x4cad 0x0d93 0x09c5 0x0a84 0x0591 0x04c5 0x058b 0x4c9c 0x0390 0x1ec5 0x0d87 0x0589 0x0591 0x0580 – Lamawy Apr 27 '15 at 13:42
  • @jamp i just put the dots because its long. – Lamawy Apr 27 '15 at 13:42
  • 1
    see.. that was an important detail... you are reading the whole file as a single number. Yes, put them in a line with commas or change the code to use a new line as delimiter – jamp Apr 27 '15 at 13:42
  • 1
    This looks related: http://stackoverflow.com/questions/11377944/parsing-a-hexadecimal-string-to-an-integer-throws-a-numberformatexception – skh Apr 27 '15 at 13:54
  • @jamp okay im sorry for that, updated. but how i write delimiter for new line? – Lamawy Apr 27 '15 at 14:18
  • 1
    Just look at all the answers here... they are correct... if it's not working, you are doing something else wrong... – jamp Apr 27 '15 at 14:23

5 Answers5

1

This will work providing all of your numbers are formatted the same

String[] tempsArray = temps.toArray(new String[74]);
int[] myIntArray = new int[tempsArray.length];
for (int i = 0; i < tempsArray.length; i++) {
  myIntArray[i] = Integer.parseInt(tempsArray[i].substring(2), 16);
  System.out.println(myIntArray[i]);
}

If it doesn't work then print out what is going into the loop and see if you are parsing the file correctly. Print one item from your string array you got from the file you parsed and feed it into this.

    String hex = "0x4c9c";
    int value = Integer.parseInt(hex.substring(2), 16);
    System.out.println(value);

Print out the output of your file parse, most likely it wont match what you are expecting.

List<String> temps = new ArrayList<String>();
while (inFile1.hasNext()) {
  token1 = inFile1.next();
  temps.add(token1);

  System.out.println(token1); //Check this output. Is it a hex string?
}
inFile1.close();
Greg King
  • 140
  • 1
  • 9
  • tried it and i got the first hex correctly : 11904 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2 note that i changed usDelimiter("[;\r\n+]"); – Lamawy Apr 27 '15 at 14:20
  • String index out of range: -2 is coming from the line tempsArray[i].substring(2). Which means you are parsing your file incorrectly. Start by printing out the output System.out.print.ln(token1) and see if it is what you expect. – Greg King Apr 27 '15 at 14:28
  • 1
    WORKED !! just removed the useDelimiter and it worked !! THANKS ALOT !! – Lamawy Apr 27 '15 at 14:43
0

There a two possibilities for your posted code

  1. you store the hex codes different in the file

    // instead of
    0x2e80
    // as
    2e80

    // and amend the code to specify the radix
    myIntArray[i] = Integer.parseInt(tempsArray[i], 16);

  2. or store them as current

    // and amend the code to remove the prefix and specify the radix
    myIntArray[i] = Integer.parseInt(tempsArray[i].substring(2), 16);

see the code snippets for both solutions

for case 1.)

List<String> temps = new ArrayList<>();
temps.add("2e80");
String[] tempsArray = temps.toArray(new String[74]);
int parseInt = Integer.parseInt(tempsArray[0], 16);
System.out.println("parseInt = " + parseInt);

for case 2.)

List<String> temps = new ArrayList<>();
temps.add("0x2e80");
String[] tempsArray = temps.toArray(new String[74]);
int parseInt = Integer.parseInt(tempsArray[0].substring(2), 16);
System.out.println("parseInt = " + parseInt);

output for both snippets is

parseInt = 11904

edit

A fixed version of the OPs code

// import java.nio.charset.Charset;
// import java.nio.file.Files;
// import java.nio.file.Paths;
// import java.util.List;

List<String> lines = Files.readAllLines(Paths.get("chipertext.txt"), 
        Charset.defaultCharset());
int[] myIntArray = new int[lines.size()];
for (int i = 0; i < myIntArray.length; i++) {
    myIntArray[i] = Integer.parseInt(lines.get(i).substring(2), 16);
}
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • @Lamawy: One thing that I have noticed that that you do not need this part: `.useDelimiter(",\\s*")` if you have each value in a separate line. – npinti Apr 27 '15 at 13:39
  • @Lamawy If you still the the same error you need to investigate what do you store into `tempsArray`. – SubOptimal Apr 27 '15 at 13:42
  • @Lamawy Have a look of the added more simplified working solution. – SubOptimal Apr 27 '15 at 13:50
  • @SubOptimal i should impory package for Path.get right ? – Lamawy Apr 27 '15 at 13:56
  • @Lamawy I updated my answer with all import statements needed for the snippet. – SubOptimal Apr 27 '15 at 13:58
  • @SubOptimal tried it and i get: Exception in thread "main" java.lang.NumberFormatException: For input string: "0x" – Lamawy Apr 27 '15 at 14:06
  • @Lamawy If you have used the posted snipped it means you have a line ` 0x` in your file. If you have not used the posted snippet, please update your question to include the code you are using and the input file which cause the NPE. Otherwise this will be a remote-guess-debugging-session. – SubOptimal Apr 27 '15 at 14:19
  • yes i have used the posted file and it does contains 0x, but what is the problem ? – Lamawy Apr 27 '15 at 14:22
  • @SubOptimal i think the mistake is here useDelimiter(",\\s*") does this read line by line ? – Lamawy Apr 27 '15 at 14:26
  • @Lamawy In the last posted snippet there is no `useDelimiter(",\\s*")`. The snippet reads the lines using the API into a `List`. If you don't want to use this approach (for what ever reason) you should update your question to provide **all** necessary information. Best would be to provide a [SSCCE](http://sscce.org/). – SubOptimal Apr 27 '15 at 14:30
  • @SubOptimal Worked, i updated my post. actually all of the answers are correct but there were a small mistake. REALLy THANK YOU FOR THE HELP !!! – Lamawy Apr 27 '15 at 14:47
0

Look at Tomasz Nurkiewicz's answer here : Parsing a Hexadecimal String to an Integer throws a NumberFormatException?

To resume you need to use

Integer.parseInt(tempsArray[i], 16)

The radix parameter sets at 16 tells parseInt to parse the String for hexa value. Prior to that, you need to delete every "0x"

Community
  • 1
  • 1
willysama
  • 66
  • 4
0

Assuming that you are storing 0x to denote it as hexadecimal number you can use Integer.parseInt method which accepts radix parameter. here radix 10 means its decimals , radix 16 means hexadecimals. and you could use the substring Method of String to remove the 0x noise .

so solution would look like below

 System.out.println(Integer.parseInt("-FF", 16) ); // prints -255 

        String [] hexaDecimalNumbers = {"0x2e80","0x008c","0x0993","0x09c5","0x058b","0x4c9c","0x0390","0x1e96","0x0989","0x0ac4","0x4cad","0x0d93","0x09c5","0x0a84","0x0591","0x04c5","0x058b","0x4c9c","0x0390","0x1ec5","0x0d87","0x0589","0x0591","0x0580","0x1fc4","0x4cb2","0x0591","0x048a","0x1991","0x4c84","0x4c8d","0x1988","0x0e89","0x09c5","0x0e90","0x18c5","0x1e80","0x0d96","0x038b","0x0d87","0x0080","0x4c86","0x038b","0x0a8c","0x0880","0x0286","0x09c5","0x058b","0x4c9c","0x0390","0x1ec5","0x0392","0x02c5","0x1c8a","0x1b80","0x1e96","0x4c9c","0x0390","0x4c86","0x0d8b","0x028a","0x18c5","0x0e80","0x4c96","0x1986","0x0f80","0x1f96","0x0a90","0x00c5","0x0397","0x4c8d","0x0d95","0x1c9c","0x42e5"};
        for(String hexaDecimal: hexaDecimalNumbers) {
            System.out.print(Integer.parseInt(hexaDecimal.substring(2), 16) + " , ");
        }

when I will run it I prints output as below

11904 , 140 , 2451 , 2501 , 1419 , 19612 , 912 , 7830 , 2441 , 2756 , 19629 , 3475 , 2501 , 2692 , 1425 , 1221 , 1419 , 19612 , 912 , 7877 , 3463 , 1417 , 1425 , 1408 , 8132 , 19634 , 1425 , 1162 , 6545 , 19588 , 19597 , 6536 , 3721 , 2501 , 3728 , 6341 , 7808 , 3478 , 907 , 3463 , 128 , 19590 , 907 , 2700 , 2176 , 646 , 2501 , 1419 , 19612 , 912 , 7877 , 914 , 709 , 7306 , 7040 , 7830 , 19612 , 912 , 19590 , 3467 , 650 , 6341 , 3712 , 19606 , 6534 , 3968 , 8086 , 2704 , 197 , 919 , 19597 , 3477 , 7324 , 17125 ,
Shirishkumar Bari
  • 2,692
  • 1
  • 28
  • 36
0

Instead of Integer.parseInt(tempsArray[i]); use Integer.parseInt(tempsArray[i].substring(2), 16));

Ramesh-X
  • 4,853
  • 6
  • 46
  • 67