4 messages in net.java.dev.jna.users[jna-users] Re: Threads from yesterday
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:[jna-users] Re: Threads from yesterdayActions...
From:Timothy Wall (twal@dev.java.net)
Date:Mar 26, 2009 6:14:33 am
List:net.java.dev.jna.users

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.