转换4个字节为无符号的32位整数,并将其储存在相当长的 [英] Convert 4 bytes to an unsigned 32-bit integer and storing it in a long

查看:245
本文介绍了转换4个字节为无符号的32位整数,并将其储存在相当长的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读的Java二进制文件。我需要的方法来读取无符号的8位值,16位无符号值和无符号32位值。什么是最好的(最快,最好找code)这样做呢?我在C ++中这样做了,做了这样的事情:

I'm trying to read a binary file in Java. I need methods to read unsigned 8-bit values, unsigned 16-bit value and unsigned 32-bit values. What would be the best (fastest, nicest looking code) to do this? I've done this in c++ and did something like this:

uint8_t *buffer;
uint32_t value = buffer[0] | buffer[1] << 8 | buffer[2] << 16 | buffer[3] << 24;

但在Java例如,如果缓冲区[1]包含有它符号位设置为左移位的结果是一个int值这会导致问题(?)。而不是OR:ING仅在0xA5的具体地点,或:■在0xFFFFA500或类似的东西,其中赔偿两顶字节

But in Java this causes a problem if for example buffer[1] contains a value which has it sign bit set as the result of a left-shift is an int (?). Instead of OR:ing in only 0xA5 at the specific place it OR:s in 0xFFFFA500 or something like that, which "damages" the two top bytes.

我有一个code现在看起来是这样的:

I have a code right now which looks like this:

public long getUInt32() throws EOFException, IOException {
    byte[] bytes = getBytes(4);
    long value = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
    return value & 0x00000000FFFFFFFFL;
}

如果我想的四个字节转换0x67 0xA5和0x72为0x50结果是0xFFFFA567而不是0x5072A567。

If I want to convert the four bytes 0x67 0xA5 0x72 0x50 the result is 0xFFFFA567 instead of 0x5072A567.

编辑:这个伟大的工程:

This works great:

public long getUInt32() throws EOFException, IOException {
    byte[] bytes = getBytes(4);
    long value = bytes[0] & 0xFF;
    value |= (bytes[1] << 8) & 0xFFFF;
    value |= (bytes[2] << 16) & 0xFFFFFF;
    value |= (bytes[3] << 24) & 0xFFFFFFFF;
    return value;
}

不过,是不是有更好的办法来做到这一点? 10位的操作似乎有点多的是这样一个简单的事情。(见我做什么呢?)=)

But isn't there a better way to do this? 10 bit-operations seems a "bit" much for a simple thing like this.. (See what I did there?) =)

推荐答案

您已经有了正确的想法,我不认为有任何明显的改善。如果你看一下 java.io.DataInput.readInt 规格,他们有code同样的事情。他们切换顺序&LT;&LT; &放大器; ,但在其他方面的标准

You've got the right idea, I don't think there's any obvious improvement. If you look at the java.io.DataInput.readInt spec, they have code for the same thing. They switch the order of << and &, but otherwise standard.

有没有办法读 INT 中从字节阵列一气呵成,除非你使用的内存-mapped地区,这是的办法的这种矫枉过正。

There is no way to read an int in one go from a byte array, unless you use a memory-mapped region, which is way overkill for this.

当然,你可以直接使用的 DataInputStream以而不是读成字节[] 第一:

Of course, you could use a DataInputStream directly instead of reading into a byte[] first:

DataInputStream d = new DataInputStream(new FileInputStream("myfile"));
d.readInt();

DataInputStream以在对面的字节序的作品比你正在使用,所以你需要一些 Integer.reverseBytes 通话也。这不会是任何更快,但它的清洁。

DataInputStream works on the opposite endianness than you are using, so you'll need some Integer.reverseBytes calls also. It won't be any faster, but it's cleaner.

这篇关于转换4个字节为无符号的32位整数,并将其储存在相当长的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆