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

5

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

我知道你可以这样写:int i = 0x

但是当读入这些值时,我无法这样做,会出现错误?

提前感谢!

5个回答

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

我喜欢这个输出 "cafe" 和 "bebe"。 - WorkaroundNewbie

1

我猜你是指ASCII十六进制?在这种情况下,没有简单的方法,但也不难。

你需要确切地知道字符串是如何存储的才能解析它们。

如果它们是像这样的:

1203 4058 a92e

那么你需要读入文件,并使用空格和换行符(空白字符)作为分隔符。

如果是:

0x1203
0x4058

这是不同的

如果它是:

12034058...

那是另一回事。

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

Integer.parseInt(string, 16)

1
对于可能具有"0x"前缀的字符串,请调用Integer.decode(String)。您可以将其与Scanner一起使用。
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;

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);
}

不幸的是,除非输入非常短,否则Scanner速度极慢。 - Alex Cohn

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接