我有这部分代码:
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);
}
它从数组中获取十六进制值并对其进行一些操作。它工作得很好,正如我所愿。
我想做同样的事情,但我想从文件中读取值并对其执行相同的操作,我尝试了以下操作:
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);
}
但我收到此错误:
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"
存储在文件中的值如下所示:
0x2e80
0x008c
0x0993
0x09c5
0x058b
0x4c9c
0x0390
0x1e96
0x0989
0x0ac4
0x4cad
0x0d93
0x09c5
0x0a84
0x0591
0x04c5
0x058b
0x4c9c
0x0390
0x1ec5
0x0d87
0x0589
0x0591
0x0580
我认为这意味着该字符串不能存储为整数,对吧?那怎么办呢?以及之前它是如何存储在整数数组中的?!我不知道有人可以帮助我吗?
工作代码
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);
}
谢谢大家的帮助!!!
最佳答案
只要您的所有号码的格式都相同,这就会起作用
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]);
}
如果它不起作用,则打印出循环中的内容并查看是否正确解析文件。打印从解析的文件中获得的字符串数组中的一项,并将其输入其中。
String hex = "0x4c9c";
int value = Integer.parseInt(hex.substring(2), 16);
System.out.println(value);
打印出文件解析的输出,很可能它与您的期望不匹配。
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();
关于java - 将 String 数组转换为 int 值并将其存储在 int 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29896937/