JuiCe
JuiCe

Reputation: 4201

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.

Upvotes: 9

Views: 6776

Answers (2)

Jevgenij Kononov
Jevgenij Kononov

Reputation: 1239

you need to import class

Class UInt32

java.lang.Object

And then you can simple use Uint32 and Uint16

Upvotes: -1

Óscar López
Óscar López

Reputation: 236034

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.

Upvotes: 7

Related Questions