Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 383
JuiCe Avatar asked Jul 18 '12 13:07

JuiCe


1 Answers

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.

like image 54
Óscar López Avatar answered Sep 24 '22 01:09

Óscar López