字节数组到Int数组 [英] byte array to Int Array
问题描述
我正在使用以下方式读取文件:
I am reading a file by using:
int len = (int)(new File(args[0]).length());
FileInputStream fis =
new FileInputStream(args[0]);
byte buf[] = new byte[len];
fis.read(buf);
我发现此处。是否可以将字节数组buf
转换为 Int数组
?将字节数组
转换为 Int数组
会占用更多空间吗?
As I found here. Is it possible to convert byte array buf
to an Int Array
? Is converting the Byte Array
to Int Array
will take significantly more space ?
编辑:我的文件包含数百万的整数,
my file contains millions of ints like,
100000000 200000000 .....(使用普通的int文件编写)。我把它读到字节缓冲区。现在我想将它包装到IntBuffer数组中。怎么做 ?我不想将每个字节转换为int。
100000000 200000000 ..... (written using normal int file wirte). I read it to byte buffer. Now I want to wrap it into IntBuffer array. How to do that ? I dont want to convert each byte to int.
推荐答案
你在评论中说过要输入四个字节数组对应于输出数组上的一个整数,因此效果很好。
You've said in the comments that you want four bytes from the input array to correspond to one integer on the output array, so that works out nicely.
取决于您是否期望字节为big-endian或little-endian顺序,但是......
Depends on whether you expect the bytes to be in big-endian or little-endian order, but...
IntBuffer intBuf =
ByteBuffer.wrap(byteArray)
.order(ByteOrder.BIG_ENDIAN)
.asIntBuffer();
int[] array = new int[intBuf.remaining()];
intBuf.get(array);
完成,分为三行。
这篇关于字节数组到Int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!