[英]Java reading hex values into an array of type int
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. 我有一个文件,其中包含以十六进制IS表示的整数,可以通过任何方式将所有这些数字存储到整数数组中。
I know you can say int i = 0x 我知道你可以说int i = 0x
but I cannot do this when reading in the values I got an error? 但是在读取出现错误的值时我不能这样做?
Thanks in advance! 提前致谢!
You probably want to go through Integer.parseInt(yourHexValue, 16)
. 您可能想要遍历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
I'm guessing you mean ascii hex? 我猜你是说ascii十六进制? 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)
For strings that may have "0x"
prefix, call Integer.decode(String) . 对于可能带有"0x"
前缀的字符串 ,请调用Integer.decode(String) 。 You can use it with Scanner , 您可以将其与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. 不幸的是,除非输入非常短,否则Scanner的速度非常慢。 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;
Read in the values as strings, and call Integer.valueOf with a radix of 16. 读取值作为字符串,然后以16为基数调用Integer.valueOf。
See javadoc here: JavaSE6 Documentation: Integer.valueOf(String, int) 请参阅以下Javadoc: JavaSE6文档:Integer.valueOf(String,int)
A Scanner
might be useful. Scanner
可能会有用。 Are the numbers in your file prefixed with 0x
? 文件中的数字是否以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: 如果它们不带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.