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 !!!

17
  • Please post how is the data stored within the file you are using. Commented Apr 27, 2015 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. Commented Apr 27, 2015 at 13:22
  • 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 Commented Apr 27, 2015 at 13:42
  • 1
    This looks related: stackoverflow.com/questions/11377944/… Commented Apr 27, 2015 at 13:54
  • 1
    Just look at all the answers here... they are correct... if it's not working, you are doing something else wrong... Commented Apr 27, 2015 at 14:23

5 Answers 5

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();
Sign up to request clarification or add additional context in comments.

3 Comments

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+]");
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.
WORKED !! just removed the useDelimiter and it worked !! THANKS ALOT !!
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 ,

Comments

0

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

Comments

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"

Comments

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);
}

11 Comments

@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.
@Lamawy If you still the the same error you need to investigate what do you store into tempsArray.
@Lamawy Have a look of the added more simplified working solution.
@SubOptimal i should impory package for Path.get right ?
@Lamawy I updated my answer with all import statements needed for the snippet.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.