9

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.

4

2 回答 2

7

这些是等价的,请记住 Java 中原始数据类型的大小始终相同,而 C 中数据类型的大小在某种程度上是编译器和体系结构特定的,正如 @millimoose 在注释。

另外,请注意,char数据类型被定义为“可以包含基本字符集的机器的最小可寻址单元。它是整数类型。实际类型可以是有符号或无符号,具体取决于实现”,而在 Java 中是单个16 位 Unicode 字符。

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

在 Java 中没有无符号的原始数据类型。该byte类型使用8位、int32位、short16位和long64位。

这是 Java 教程中相关部分的链接,在Java 语言规范的第 4.2 节中有更详细的解释。

于 2012-07-18T14:01:03.330 回答
-1

你需要导入类

UInt32 类

java.lang.Object

然后你可以简单地使用 Uint32 和 Uint16

于 2016-06-27T14:42:14.930 回答