将 String 数组转换为 int 值并将其存储在 int 数组中
Convert String array to int values and store it in int array
我有这部分代码:
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);
}
谢谢大家的帮助!!!
您发布的代码有两种可能性
你在文件中存储的十六进制代码不同
// 而不是
0x2e80
// 作为
2e80
// 并修改代码以指定基数
myIntArray[i] = Integer.parseInt(tempsArray[i], 16);
或将它们存储为当前
// 并修改代码去掉前缀指定基数
myIntArray[i] = Integer.parseInt(tempsArray[i].substring(2), 16);
查看两种解决方案的代码片段
对于案例 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);
对于案例 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);
两个片段的输出是
parseInt = 11904
编辑
OP 代码的固定版本
// 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);
}
如果您的所有号码格式相同,这将有效
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();
在这里查看 Tomasz Nurkiewicz 的回答:Parsing a Hexadecimal String to an Integer throws a NumberFormatException?
要恢复你需要使用
Integer.parseInt(tempsArray[i], 16)
设置为 16 的基数参数告诉 parseInt 解析 String 以获得十六进制值。在此之前,您需要删除每个“0x”
假设您正在存储 0x
以将其表示为十六进制数,您可以使用接受 radix
参数的 Integer.parseInt 方法。这里 radix 10 表示它的小数,radix 16 表示十六进制。并且您可以使用字符串的子字符串方法来删除 0x
噪音 .
所以解决方案如下所示
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) + " , ");
}
当我将 运行 它打印输出如下
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 ,
而不是 Integer.parseInt(tempsArray[i]);
使用 Integer.parseInt(tempsArray[i].substring(2), 16));
我有这部分代码:
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);
}
谢谢大家的帮助!!!
您发布的代码有两种可能性
你在文件中存储的十六进制代码不同
// 而不是
0x2e80
// 作为
2e80// 并修改代码以指定基数
myIntArray[i] = Integer.parseInt(tempsArray[i], 16);或将它们存储为当前
// 并修改代码去掉前缀指定基数
myIntArray[i] = Integer.parseInt(tempsArray[i].substring(2), 16);
查看两种解决方案的代码片段
对于案例 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);
对于案例 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);
两个片段的输出是
parseInt = 11904
编辑
OP 代码的固定版本
// 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);
}
如果您的所有号码格式相同,这将有效
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();
在这里查看 Tomasz Nurkiewicz 的回答:Parsing a Hexadecimal String to an Integer throws a NumberFormatException?
要恢复你需要使用
Integer.parseInt(tempsArray[i], 16)
设置为 16 的基数参数告诉 parseInt 解析 String 以获得十六进制值。在此之前,您需要删除每个“0x”
假设您正在存储 0x
以将其表示为十六进制数,您可以使用接受 radix
参数的 Integer.parseInt 方法。这里 radix 10 表示它的小数,radix 16 表示十六进制。并且您可以使用字符串的子字符串方法来删除 0x
噪音 .
所以解决方案如下所示
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) + " , ");
}
当我将 运行 它打印输出如下
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 ,
而不是 Integer.parseInt(tempsArray[i]);
使用 Integer.parseInt(tempsArray[i].substring(2), 16));