What I want to do is to call a C function that has an array of bytes as one of
the arguments:
//method in C; y is the array of UCHARs
bool method(UCHAR x, UCHAR *y)
//method in Java
boolean method(byte x, byte [] y)
-----Ursprüngliche Nachricht-----
Von: Timothy Wall [mailto:twal...@dev.java.net]
Gesendet: Donnerstag, 26. März 2009 14:15
An: use...@jna.dev.java.net
Betreff: [jna-users] Re: Threads from yesterday
On Mar 26, 2009, at 4:59 AM, Novatchkov Hristo wrote:
Hi Timothy,
I'm sorry for mentioning the topic again, but is there really no
direct way of mapping of C's unsigned types in Java? I would
especially need to convert an UCHAR holding a hex value from C in
Java, like:
// C code
UCHAR test = 0xA1;
// Java code
byte test = (byte) 0xA1;
The Java variable, though, has a negative integer value, while the
one in C has a positive one. Therefore the code above wouldn't
represent a direct mapping. Could you please give me any hints?
You could use a type mapper to convert the byte array into a short or
int array in Java, in which case you'd be able to have positive values
instead of negative ones, but that still doesn't let you do unsigned 8-
bit arithmetic properly without extra work. The easiest thing is to
convert the byte into a larger type when you want to look at it as a
0-255 value, e.g.
short value = ((short)byte_array[idx]) & 0xFF;
You still haven't said why you want them to be "unsigned" in Java
space. What you want to do with the values will greatly affect the
choice of the best representation. Unsigned behavior encompasses much
more than simply how a bit pattern is displayed.