java Java将十六进制值读入int类型的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3834468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java reading hex values into an array of type int
提问by Bob
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
我知道你可以说 int i = 0x
but I cannot do this when reading in the values I got an error?
但是在读入错误值时我不能这样做?
Thanks in advance!
提前致谢!
回答by aioobe
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
回答by Alex Cohn
For strings that may have "0x"
prefix, call Integer.decode(String). You can use it with Scanner,
对于可能有"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);
}
Unfortunately, unless the input is very short, Scanneris ridiculously slow. Here is an efficient hand-made parser for hexadecimal strings:
不幸的是,除非输入非常短,否则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;
回答by Bill K
I'm guessing you mean ascii hex? In that case there isn't a trivial way, but it's not hard.
我猜你的意思是 ascii 十六进制?在这种情况下,没有什么简单的方法,但并不难。
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)
回答by Richard Fearn
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:
AScanner
可能有用。您文件中的数字是否以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);
}
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);
}
回答by Starkey
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)