简体   繁体   中英

Most elegant way to convert a byte to an int in Java

Example code:

int a = 255;
byte b = (byte) a;
int c = b & 0xff; // Here be dragons
System.out.println(a);
System.out.println(b);
System.out.println(c);

So we start with an integer value of 255, convert it to a byte (becoming -1) and then converting it back to an int by using a magic formula. The expected output is:

255
-1
255

I'm wondering if this a & 0xff is the most elegant way to to this conversion. checkstyle for example complains about using a magic number at this place and it's not a good idea to ignore this value for this check because in other places 255 may really be a magic number which should be avoided. And it's quite annoying to define a constant for stuff like this on my own. So I wonder if there is a standard method in JRE which does this conversion instead? Or maybe an already defined constant with the highest unsigned byte value (similar to Byte.MAX_VALUE which is the highest signed value)

So to keep the question short: How can I convert a byte to an int without using a magic number?

Ok, so far the following possibilities were mentioned:

  • Keep using & 0xff and ignore the magic number 255 in checkstyle. Disadvantage: Other places which may use this number in some other scope (not bit operations) are not checked then, too. Advantage: Short and easy to read.
  • Define my own constant for it and then use code like & SomeConsts.MAX_UNSIGNED_BYTE_VALUE . Disadvantage: If I need it in different classes then I have to define my own constant class just for this darn constant. Advantage: No magic numbers here.
  • Do some clever math like b & ((1 << Byte.SIZE) - 1) . The compiler output is most likely the same because it gets optimized to a constant value. Disadvantage: Pretty much code, difficult to read. Advantage: As long as 1 is not defined as magic number (checkstyle ignores it by default) we have no magic number here and we don't need to define custom constants. And when bytes are redefined to be 16 bit some day (Just kidding) then it still works because then Byte.SIZE will be 16 and not 8.

Are there more ideas? Maybe some other clever bit-wise operation which is shorter then the one above and only uses numbers like 0 and 1?

This is the standard way to do that transformation. If you want to get rid of the checkstyle complaints, try defining a constant, it could help:

 public final static int MASK = 0xff;

BTW - keep in mind, that it is still a custom conversion. byte is a signed datatype so a byte can never hold the value 255 . A byte can store the bit pattern 1111 1111 but this represents the integer value -1 .

So in fact you're doing bit operations - and bit operations always require some magic numbers.


BTW-2 : Yes, there is a Byte.MAX_VALUE constant but this is - because byte is signed - defined as 2 7 -1 (= 127). So it won't help in your case. You need a byte constant for -1.

Ignore checkstyle. 0xFF is not a magic number. If you define a constant for it, the constant is a magic constant, which is much less understandable than 0xFF itself. Every programmer educated in the recent centuries should be more familiar with 0xFF than with his girlfriend, if any.

should we write code like this?

for(int i = Math.ZERO; ... )

I wrote a method for this like

public static int unsigned(byte x) {
    return int (x & 0xFF);
}

which is overloaded for short and int parameters, too (where int gets extended to long).

Instead of 0xFF you could use Byte.MAX_VALUE+Byte.MAX_VALUE+1 to keep FindBug shut, but I'd consider it to be an obfuscation. And it's too easy to get it wrong (s. previous versions).

Java 8 provides Byte.toUnsignedInt and Byte.toUnsignedLong (probably for really big bytes) methods:

byte b = (byte)255;
int c = Byte.toUnsignedInt(b); // 255
long asLong = Byte.toUnsignedLong(b); // 255

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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