Question

I know Java byte range is -128 to 127, but C++ BYTE is a unsigned char (0 to 255.)

I want to convert a C++ function to Java, and in C++ I have a LPBYTE (which is a pointer to BYTE array.) I've tried using byte array instead of LPBYTE in C++ function, but my Java function is still not working correctly.

My C++ function works on a BMP file and it's algorithm is customized run length encoding. In converting it to Java I'm using a BufferedImage and getting its content as byte[]. I'm guessing the Java function is not working correctly because of type mismatch.

Which type should I use in Java?

Was it helpful?

Solution

In Java you can use a plain byte and convert to an int as required with b & 0xFF

The problem you have is that Java does not support pointers and you cannot pass a pointer to bytes to Java. The nearest equivalent is byte[].

If you are only doing RLE encoding, the actual value of the bytes is not important so it doesn't matter whether they are signed or unsigned. If you are decoding a BMP file you can use the mask as above as required.

When reading/writing to a byte array, a common solution is to use a ByteBuffer. This supports writing longer types such as short or int in little endian or big endian order and you don't need to know the exact length in advance.

OTHER TIPS

Java has no unsigned types (apart from char which is 16 bits). So you have no choice, pick byte but rewrite your code so it works with signed integers. If you need help with this post the code.

There is no unsigned byte type in Java. You could use short instead. This can represent all values from 0 to 255, and more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top