github twitter linkedin
JNA and Unsigned Integers

I’ve written an Argon2 binding for the JVM (in case you’re interested, you can find it here). This binding uses JNA to call the Argon2 C library. The C library uses the uint32_t datatype, that’s an unsigned 32-bit integer. The JNA documentation states:

Unsigned values may be passed by assigning the corresponding two’s-complement representation to the signed type of the same size.

Haha, you expect me to bitshift my integers around to get rid of the two’s-complement representation?

I’ve found a simpler way: JNA provides an abstract class called IntegerType. If you want an unsigned 32-bit integer, just create this class:

public class Uint32_t extends IntegerType {
    public Uint32_t() {

    public Uint32_t(int value) {
        super(4, value, true);

The first constructor is needed because JNA (otherwise it fails at runtime), the second constructor accepts the value of the unsiged integer. The 4 is 32 bit in bytes, the true argument tells the superclass to handle the value as an unsigned integer.

You can use this trick to pass 8-bit, 16-bit, 32-bit and 64-bit values without hassle.

Back to posts