Java将十六进制值读入int类型的数组中

时间:2010-09-30 20:14:02

标签: java

我有一个文件,其中包含以十六进制表示的整数 有没有办法将所有这些数字存储到整数数组中。

我知道你可以说 int i = 0x

但是在读取错误的值时我不能这样做?

提前致谢!

5 个答案:

答案 0 :(得分:6)

您可能想要浏览Integer.parseInt(yourHexValue, 16)

示例:

// Your reader
BufferedReader sr = new BufferedReader(new StringReader("cafe\nBABE"));

// Fill your int-array
String hexString1 = sr.readLine();
String hexString2 = sr.readLine();

int[] intArray = new int[2];
intArray[0] = Integer.parseInt(hexString1, 16);
intArray[1] = Integer.parseInt(hexString2, 16);

// Print result (in dec and hex)
System.out.println(intArray[0] + " = " + Integer.toHexString(intArray[0]));
System.out.println(intArray[1] + " = " + Integer.toHexString(intArray[1]));

<强>输出:

51966 = cafe
47806 = babe

答案 1 :(得分:1)

我猜你的意思是ascii hex?在这种情况下,没有一种微不足道的方式,但并不难。

您需要准确了解字符串的存储方式才能解析它们。

如果他们是这样的话:

1203 4058 a92e

然后你需要读取文件并使用空格和换行符(空格)作为分隔符。

如果是:

0x1203
0x4058

那是不同的

如果是的话:

12034058...

那是别的。

弄清楚如何将其变为字符串,其中每个字符串仅包含单个数字的十六进制数字,然后调用

Integer.parseInt(string, 16)

答案 2 :(得分:0)

将值读入字符串,并以16的小数调用Integer.valueOf。

请在此处查看javadoc:JavaSE6 Documentation: Integer.valueOf(String, int)

答案 3 :(得分:0)

Scanner可能有用。您文件中的数字是否以0x为前缀?如果是这样,你将不得不删除它并转换为整数:

// using StringReader for illustration; in reality you'd be reading from the file
String input = "0x11 0x22 0x33";
StringReader r = new StringReader(input);

Scanner s = new Scanner(r);
while (s.hasNext()) {
    String hexnum = s.next();
    int num = Integer.parseInt(hexnum.substring(2), 16);
    System.out.println(num);
}

如果它们没有0x的前缀,那就更简单了:

String input = "11 22 33";
StringReader r = new StringReader(input);

Scanner s = new Scanner(r);
while (s.hasNext()) {
    int num = s.nextInt(16);
    System.out.println(num);
}

答案 4 :(得分:0)

对于可能带有"0x"前缀的字符串,请调用 Integer.decode(String)。您可以将其与扫描仪

try (Scanner s = new Scanner("0x11 0x22 0x33")) {
  while (s.hasNext()) {
    int num = Integer.decode(s.next());
    System.out.println(num);
  }
}
catch (Exception ex) {
  System.out.println(ex);
}

不幸的是,除非输入非常短,否则 Scanner 的运行速度非常慢。这是一个有效的十六进制字符串手工解析器:

static boolean readArray(InputStream stream, int[] array) {

  int i = 0;

  final int SPACE = 0;
  final int X = 1;
  final int HEXNUM = 2;
  final int ERROR = -1;

  for (int next_char= -1, expected = SPACE; i <= array.length && stream.available() > 0 && expected != ERROR; next_char = stream.read()) {

    switch (expected)  {

      case SPACE:
        if (Character.isWhitespace(next_char))
          ;
        else if (next_char == '0') {
          array[i] = 0;
          expected = X;
        }
        else {
          LOGGER.e("unexpected '" + next_char + "' for " + expected + " at " + i);
          expected = ERROR;
        }
        break;

      case X:
        if (next_char == 'x' || next_char == 'X') {
          expected = HEXNUM;
        }
        else {
          LOGGER.e("unexpected '" + next_char + "' for " + expected + " at " + i);
          expected = ERROR;
        }
        break;

      case HEXNUM:
        if (Character.isDigit(next_char)) {
          array[i] *= 16;
          array[i] += next_char - '0';
        }
        else if (next_char >= 'a' && next_char <= 'f') {
          array[i] *= 16;
          array[i] += next_char - 'a' + 10;
        }
        else if (next_char >= 'A' && next_char <= 'F') {
          array[i] *= 16;
          array[i] += next_char - 'A' + 10;
        }
        else if (Character.isWhitespace(next_char)) {
          i++;
          expected = SPACE;
        }
        else {
          LOGGER.e("unexpected '" + next_char + "' for " + expected + " at " + i);
          expected = ERROR;
        }
    }
  }
}
if (expected == ERROR || i != array.length) {
  LOGGER.w("read " + i + " hexa integers when " + array.length + " were expected");
  return false;
}
return true;