考虑这样的代码:
public static void main(String[] args) {
String One = "f9 d4 62 aa f9 d4 62 aa 74 10 99 8b";
String Two = "3c 9c a7 e2 3c 9c a7 e2 b1 58 f9 d4";
int[] x = convert(One);
int[] y = convert(Two);
for (int i = 0; i < x.length; i++) {
int number = x[i];
int index = find(number, y);
if (index > 0) {
System.out.println("found (" + (i + 1) + "," + index + ")");
}
}
}
private static int find(int number, int[] array) {
for (int i = 0; i < array.length; i++) {
if (array[i] == number) {
return i + 1;
}
}
return 0;
}
private static int[] convert(String str) {
String[] tokens = str.split("\\s");
int[] result = new int[tokens.length/2];
for (int i = 0; i < tokens.length; i += 2) {
String hex = tokens[i] + tokens[i + 1];
result[i/2] = Integer.parseInt(hex, 16);
}
return result;
}
输出:
found (1,6)
found (3,6)
正如你可以看到convert(str)
方法每4个十六进制数字转换为1点的整数并返回这样的整数数组。所以,转换(一)仅仅是INT []等于:
System.out.println(Arrays.toString(x));
[63956, 25258, 63956, 25258, 29712, 39307]
接下来,您可以实现的辅助方法find()方法返回其中number是在给定的数组(1开始的索引)创建索引。
嗯,你为什么不要输出'(1,6)(3,6)'?有多场比赛时你在找什么?什么时候没有任何比赛? – 2014-09-26 12:13:04
用转换前的字节做这件事比处理字符串要容易。 – 2014-09-26 12:13:49
@ chiastic-security,是的,实际的字符串包含多对,我需要我的输出显示,因为你已经提到。 (1,6)和(3,6)。 – vbenthu 2014-09-26 12:17:30