簡體   English   中英

Java將十六進制值讀入int類型的數組

[英]Java reading hex values into an array of type int

我有一個文件,其中包含以十六進制IS表示的整數,可以通過任何方式將所有這些數字存儲到整數數組中。

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

但是在讀取出現錯誤的值時我不能這樣做?

提前致謝!

您可能想要遍歷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

我猜你是說ascii十六進制? 在這種情況下,沒有平凡的方法,但這並不困難。

您需要確切地知道字符串的存儲方式才能解析它們。

如果他們是這樣的:

1203 4058 a92e

那么您需要讀入文件,並使用空格和換行符(空格)作為分隔符。

如果它是:

0x1203
0x4058

那不一樣

並且如果是:

12034058...

那是另外一回事。

弄清楚如何將其分成字符串,其中每個字符串僅包含單個數字的十六進制數字,然后調用

Integer.parseInt(string, 16)

對於可能帶有"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;

讀取值作為字符串,然后以16為基數調用Integer.valueOf。

請參閱以下Javadoc: JavaSE6文檔:Integer.valueOf(String,int)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM