JuiCe JuiCe - 3 months ago 17x
Java Question

Type Conversion from C to Java

I did my best to Google this but couldn't find a clean answer in a table-like form that shows type conversions.

The reason I would like to convert these types, is because I am using the Android NDK to call functions from native code. The problem is that the native code calls different types that do not exist in Java.

I actually have no experience in C, and have found these few types from looking at code quickly. Please feel free to edit this post to add different types to be converted.

From C to Java

long ->

short ->

char ->

unsigned long ->

unsigned short ->

unsigned char ->

byte ->

Int8 ->

Int16 ->

Int32 ->

UInt8 ->

UInt16 ->

UInt32 ->

Also, if any of these cannot convert into a Java type, please explain why.


These are the equivalences, bearing in mind that the size of a primitive data type in Java is always the same, whereas the size of a data type in C is to some extent compiler-and architecture-specific, as pointed by @millimoose in the comments.

Also, be aware that the char data type is defined as "smallest addressable unit of the machine that can contain basic character set. It is an integer type. Actual type can be either signed or unsigned depending on implementation", whereas in Java is a single 16-bit Unicode character.

long -> long
short -> short
char -> char
unsigned long -> N/A
unsigned short -> N/A
unsigned char -> N/A
byte -> byte
Int8 -> byte
Int16 -> short
Int32 -> int
UInt8 -> N/A
UInt16 -> N/A
UInt32 -> N/A

In Java there are no unsigned primitive data types. The byte type uses 8 bits, int 32 bits, short 16 bits and long 64 bits.

Here's a link to the relevant section in the Java tutorial, and a more detailed explanation in section ยง4.2 of the Java Language Specification.