5

I have a file which contains integer numbers represented in hexadecimal IS there any way to store all of these numbers into an array of integers.

I know you can say int i = 0x

but I cannot do this when reading in the values I got an error?

Thanks in advance!

5 Answers 5

6

You probably want to go through Integer.parseInt(yourHexValue, 16).

Example:

// 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]));

Output:

51966 = cafe
47806 = babe
Sign up to request clarification or add additional context in comments.

1 Comment

I like this output "cafe" and "bebe"
1

I'm guessing you mean ascii hex? In that case there isn't a trivial way, but it's not hard.

You need to know exactly how the strings are stored in order to parse them.

IF they are like this:

1203 4058 a92e

then you need to read the file in and use spaces and linefeeds (whitespace) as separators.

If it's:

0x1203
0x4058

That's different yet

and if it's:

12034058...

That's something else.

Figure out how to get it into strings where each string ONLY contains the hex digits of a single number then call

Integer.parseInt(string, 16)

Comments

1

For strings that may have "0x" prefix, call Integer.decode(String). You can use it with 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);
}

Unfortunately, unless the input is very short, Scanner is ridiculously slow. Here is an efficient hand-made parser for hexadecimal strings:

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;

Comments

0

A Scanner might be useful. Are the numbers in your file prefixed with 0x? If so you'll have to remove that and convert to an integer:

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

If they're not prefixed with 0x it's even simpler:

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

1 Comment

Unfortunately, unless the input is very short, Scanner is ridiculously slow.
0

Read in the values as strings, and call Integer.valueOf with a radix of 16.

See javadoc here: JavaSE6 Documentation: Integer.valueOf(String, int)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.