简体   繁体   English

如何在Java中存储无符号字节的存储(数组)以对其进行位操作?

[英]how to store store (an array of) unsigned bytes in Java to do bit manipulations on them?

I need to store (an array of) unsigned bytes in Java to do bit manipulations on them. 我需要在Java中存储(数组)无符号字节来对它们进行位操作。 I want to do standard bit manipulations. 我想进行标准的位操作。 shift ((sVal>>8) & 0xff); 移位((sVal>>8) & 0xff); , or |, and &, and comparisons. ,或|,以及&和比较。 and things like w |= ~0xff; w |= ~0xff; How do I store them? 如何储存它们?

byte b = 0x96; 字节b = 0x96; //the value is 150 //值是150

I get a compile error: Type mismatch: cannot convert from int to byte 我收到一个编译错误:类型不匹配:无法从int转换为byte

so I cast it 所以我投了

byte b = (cast) 0x96; 字节b =(cast)0x96;

Now I see the value has changed to -106. 现在,我看到该值已更改为-106。

If I store as a char, that will promote it to 2 bytes. 如果我将其存储为字符,则会将其提升为2个字节。

How should I store the value as an unsigned byte in Java so that I can do bit manipulations on them? 如何在Java中将值存储为无符号字节,以便可以对它们进行位操作?

Will there be issues using int ? 使用int会有问题吗?

There is no unsigned primitive data type in Java -- bytes are signed. Java中没有未签名的原始数据类型-字节已签名。 You'll need to figure out some other way to represent your data, depending on what you want to do. 您需要根据您想做的事情找出其他表示数据的方法。

EDIT: 编辑:

People have given you a number of suggestions and explanations, and you haven't explained much about the requirements of your situation ('a lot of them and fast' isn't helpful). 人们为您提供了许多建议和解释,而您对情况的要求则没有太多解释(“很多而且快速”无济于事)。 But I noticed something else about your original post. 但是我注意到了有关您原始帖子的其他信息。

You say 你说

byte b = (cast) 0x96;

Now I see the value has changed to -106. 现在,我看到该值已更改为-106。

I'm wondering if you understand that the value of the byte didn't change at all. 我想知道您是否了解字节的值完全没有变化。 The byte is still hexadecimal 96, and you could see that if you printed it out in hexadecimal. 该字节仍为十六进制96,如果以十六进制打印出来,您会看到。 The -106 is merely the decimal number represented by hex 96. -106只是十六进制代表的十进制数。

If ALL you want to do is bit manipulation, then you could STILL store it all as bytes. 如果您只想进行位操作,则可以将其全部存储为字节。 Only if you also need arithmetic operations -- add, subtract, multiple, divide, greater than, less than -- would you need to worry about the signed nature of the bytes. 仅当您还需要算术运算(加,减,乘,除,大于,小于)时,才需要担心字节的有符号性质。

There is no problem using bit operations (apart from unsigned-shift-right) if you are not doing sign involving arithmetics. 如果您不进行涉及算术的符号运算,则使用位运算(除了unsigned-shift-right之外)没有问题。

Yes, converting to char or int extends the sign, but you can do: 是的,转换为char或int可以扩展符号,但是您可以执行以下操作:

byte b = (byte)0x96;
int n = 0xFF & b; // Again 0..255

(Maybe one should warn, trying to store bytes in a String with chars, is a kind of abuse involving encoding conversion.) (也许有人应该警告,尝试使用chars将字节存储在String中是一种涉及编码转换的滥用。)

By the way, the classes Integer, Long and such have many interesting bit operations. 顺便说一下, Integer,Long等类具有许多有趣的位操作。 The class BitSet is also an interesting playground. BitSet类也是一个有趣的游乐场。

You can do bit manipulations on signed bytes--they produce the same results you'd expect. 您可以对带符号字节进行位操作-它们产生与预期相同的结果。 The one thing you have to watch for is >> ; 您必须注意的一件事是>> ; this will fill in the leftmost bits with 1 if the signed byte is negative, but if you use >>> (three > signs), it will always fill the leftmost bits with 0, which in essence treats it like an unsigned byte even though it's signed. 如果有符号字节为负,则它将用1填充最左边的位,但是如果使用>>> (三个>符号),它将始终用0填充最左边的位,这实际上将其视为无符号字节,即使它已签名。

If you want the integer value of a byte and you want a value from 0 to 255: 如果您想要一个字节的整数值,并且想要一个0到255之间的值:

int value = ((int)yourByte) & 0xFF;

Or, in Java 8, you can use Byte.toUnsignedInt . 或者,在Java 8中,可以使用Byte.toUnsignedInt

[Actually, the cast isn't necessary, because yourByte and 0xFF will automatically get promoted to int before the & is applied. [实际上, yourByte不是必需的,因为在应用&之前, yourByte0xFF会自动提升为int However, I never remember that rule without looking it up, so the explicit cast is helpful to me.] 但是,我从来不记得没有查找过该规则,因此显式强制转换对我很有帮助。]

If you want to do an ordered comparison of two bytes (or one byte and an integer literal) and treat them as unsigned, you can convert them both to int using one of the above methods and compare the int s. 如果要对两个字节(或一个字节和一个整数文字)进行有序比较,并将它们视为无符号,则可以使用上述方法之一将它们都转换为int并比较int

You can use binary operations on bytes with no restrictions, just need to be careful with the right shift, depending on what you are willing to achieve. 您可以无限制地对字节使用二进制操作,只需要小心选择正确的移位,具体取决于您要实现的目标。 With Integer.toBinaryString(int i) you can see what is happening right there, although it is not the best representation: 使用Integer.toBinaryString(int i)您可以看到那里发生了什么,尽管它不是最好的表示形式:

public static void main(String[] args) {
        byte b1 = (byte) 0x96;
        int i1 = b1 & 0xFF;
        System.out.println(Integer.toBinaryString(i1));
        byte b2 = (byte) (b1 << 1);
        int i2 = b2 & 0xFF;
        System.out.println(Integer.toBinaryString(i2));
            System.out.println(Integer.toBinaryString(i1 & i2));
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何存储字节并将其读回数组 - How to store bytes and read them back to an array 将String转换为二进制整数,进行位操作,然后将该二进制值再次存储为Java中的String - Convert String into binary integer, do bit manipulations and again store that binary value as String in Java 如何在java中存储unsigned short? - How to store unsigned short in java? 如何读取整数并将其存储在Java中的数组中 - how to read integers and store them in an array in java 在Java中,如何使用循环实例化新对象,将它们存储在数组中,然后使用它们? - In Java, how do I use a loop to instantiate new objects, store them in an array, and then use them? 在 Java 中,如何从 xml 中检索数据值并将它们存储在数组中? - In Java, how do I retrieve the data values from the xml and store them in an array? 如何在Eclipse Java调试器中将char数组显示为十六进制字节或无符号十进制数字的数组? - How do I display a char array as an array of hex bytes or unsigned decimal numbers in the Eclipse Java debugger? 如何在 Eclipse Java 调试器中将字节数组显示为十六进制字节或无符号十进制数的数组? - How do I display a byte array as an array of hex bytes or unsigned decimal numbers in the Eclipse Java debugger? Java 字节数组中的无符号字节 - unsigned bytes in Java byte array 如何从文件中读取行并将其存储到数组中 - How do I read lines from a file and store them into an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM