4 messages in net.java.dev.jna.usersAW: [jna-users] Re: Unsigned types
FromSent OnAttachments
Timothy WallMar 26, 2009 6:14 am 
Novatchkov HristoMar 26, 2009 7:12 am 
LYou...@gkservices.comMar 26, 2009 7:56 am 
Novatchkov HristoMar 27, 2009 2:57 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:AW: [jna-users] Re: Unsigned typesActions...
From:Novatchkov Hristo (hris@univie.ac.at)
Date:Mar 26, 2009 7:12:18 am
List:net.java.dev.jna.users

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.