0

I use a native C function from a dll like that:

short sSmartISOEx(USHORT usInDataLen, const UCHAR *pucDataIn, USHORT *pusOutDataLen, UCHAR *pucDataOut);

This function is declared in my java like that:

short sSmartISOEx(short usInDataLen, ByteBuffer pucDataIn, ShortBuffer pusOutDataLen, ByteBuffer pucDataOut);

So to use it i send a ByteBuffer pucDataIn with a lenght of usInDataLen. And normaly i retrieve a ByteBuffer pucDataOut with a lenght of pusOutDataLen.

The function work correctly, but my problem is after retrieving the ByteBuffer pucDataOut, i do something like that:

System.out.println("first byte: " + pucDataOut.get(0));
System.out.println("second byte: " + pucDataOut.get(1));

In my specifice case normaly "First byte" value is 0x69 and "Second byte" value is 0x86 But when i execute my code, this is what i obtain:

first byte: 105
second byte: -122

For 105 it's ok, it correspond to 0x69 But for -122, it correspond to 0xffffff86

Why this "pucDataOut.get(1)" correspond to 4 byte and not just only one byte. How to obtain exactly a byte array of two values 0x69 and 0x86

  • 3
    Bytes are signed in Java. Anything 0x7F higher than is negative – g00se Jan 10 '23 at 00:47
  • Printing out byte values in Java isn't useful and doesn't reflect the value you'd expect. Generally, you should operate on `myValue & 0xFF` and not care about their original numeric value. – Louis Wasserman Jan 10 '23 at 01:04
  • `System.out.println(HexFormat.ofDelimiter(" ").formatHex(pucDataOut.array()));` will show you the hex values (where numerical sign is irrelevant) – g00se Jan 10 '23 at 12:30

0 Answers0