我有这部分代码:
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);
}
谢谢大家的帮助!!!
答案 0 :(得分:1)
这样可以使您的所有数字格式相同
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();
答案 1 :(得分:0)
您发布的代码有两种可能性
您将不同的十六进制代码存储在文件
中 //而不是
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
修改强>
OPs代码的固定版本
// 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);
}
答案 2 :(得分:0)
请看Tomasz Nurkiewicz的答案:Parsing a Hexadecimal String to an Integer throws a NumberFormatException?
要恢复,您需要使用
Integer.parseInt(tempsArray[i], 16)
radix参数设置为16,告诉parseInt解析String的hexa值。在此之前,您需要删除每个&#34; 0x&#34;
答案 3 :(得分:0)
假设您正在存储 Protected Sub txtIdType_TextChanged(sender As Object, e As EventArgs)
Dim txtb As TextBox = CType(FormViewPerson.FindControl("txtIdType"), TextBox)
Dim txtb1 As TextBox = CType(FormViewPerson.FindControl("TextBoxIDCode"), TextBox)
If (txtb.Text = "Leternjoftimi" OrElse txtb.Text = "KosovoIDCard" OrElse txtb.Text = "Licna karta") Then
txtb1.MaxLength = 10
End If
End Sub
以将其表示为十六进制数,您可以使用接受0x
参数的Integer.parseInt方法。基数10表示小数,基数16表示十六进制。并且您可以使用子字符串String of String来删除radix
噪音。
所以解决方案如下所示
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) + " , ");
}
答案 4 :(得分:0)
而不是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]);