简体   繁体   English

将无符号字节转换为有符号字节

[英]Convert unsigned byte to signed byte

Is there an easy and elegant way to convert an unsigned byte value to a signed byte value in java? 有没有一种简单而优雅的方法可以将无符号字节值转换为java中的有符号字节值? For example, if all I have is the int value 240 (in binary (24 bits + 11110000) = 32bits), how can I get the signed value for this int? 例如,如果我只有int值240(二进制(24位+ 11110000)= 32位),我怎样才能得到这个int的有符号值?

In Java all the primitive types, except for char , are signed. 在Java中,除char之外的所有原始类型都是有符号的。 You can't have an unsigned byte. 你不能有一个无符号字节。 The only thing you may do is to cast the unsigned byte to an int so you can read its proper value: 您可以做的唯一事情是将无符号字节转换为int,以便您可以读取其正确的值:

int a = b & 0xff

If you want to store an unsigned byte value in a byte type, you obviously can, but every time you need to "process" it, just remember to cast it again as showed above. 如果你想在一个字节类型中存储一个无符号字节值,你显然可以,但每次你需要“处理”它时,只需记住如上所示再次投射它。

Java does not have unsigned values, except for char . 除了char之外,Java没有无符号值。 Consider this snippet: 请考虑以下代码段:

byte val = (byte)255;
System.out.println(String.valueOf(val));

The result will be -1, because the lowest 8 bits got copied over to the byte variable. 结果将为-1,因为最低的8位被复制到字节变量。

public int getUnsignedByte(byte[] bytes, int offset) {
    return (bytes[offset] & 0xFF);
}

should do the work. 应该做的工作。

Here's how to store and convert an unsigned byte value: 以下是存储和转换无符号字节值的方法:

byte b = (byte) 144; // binary value = 10010000

If you cast b to an int here's what happens: 如果你将b转换为int ,那么会发生什么:

int i = b;
System.out.println("value: "+i);  
System.out.println("binary: " + Integer.toBinaryString(i));

Output: 输出:

value: -112 <- Incorrect value
binary: 11111111111111111111111110010000 

Why does this happen? 为什么会这样? Bytes are signed in Java, and b is negative so the cast operation fills the resulting int with 1s from the left. 字节用Java签名, b是负数,因此强制转换操作从左边用1s填充结果int。 More on java data formats . 更多关于java数据格式

So how do we get back from -112 to the correct value of 144? 那么我们如何从-112回到144的正确值呢? We need a 32-bit bitmask , which we can create with an int : 我们需要一个32位的位掩码 ,我们可以用int创建它:

int mask = 0x000000FF; // This is often shortened to just 0xFF

Now we can use the bitwise & operator to "zero out" the left most 24 bits, leaving only our original 8 bits. 现在我们可以使用bitwise &运算符将最左边的24位“清零”,只保留原来的8位。

int unsignedValue = b & mask; // value = 144

Our original value of 144 has been restored and can be safely cast back to a byte: 我们的原始值144已经恢复,可以安全地转换回一个字节:

byte original = (byte) unsignedValue;

Java only supports signed bytes so whenever you place a value in a byte, its assumed to be signed. Java只支持有符号字节,所以每当你在一个字节中放置一个值时,它就会被认为是有符号的。

byte b = (byte) 240;

However if you want to store an unsigned byte, you need to handle this yourself. 但是,如果要存储无符号字节,则需要自己处理。 (ie Java doesn't support it but you can do it) (即Java不支持它,但你可以这样做)

For operations like +, -, *, <<, >>>, ==, !=, ~ you don't need to change anything, For operations like <, > you need to have make minor adjustments, and for operations like /, % you need to use a larger data type. 对于像+, - ,*,<<,>>>,==,!=,〜这样的操作,你不需要改变任何东西,对于像<,>这样的操作,你需要进行微小的调整,以及像/,%您需要使用更大的数据类型。

A common alternative is to use a larger data type like int to store values 0 - 255. There is not much disadvantage in doing so. 一种常见的替代方法是使用更大的数据类型(如int来存储值0到255.这样做没有太大的缺点。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM