简体   繁体   English

Objective-C和Java原始数据类型

[英]Objective-C and Java Primitive Data Types

I need to convert a piece of code from Objective-C to Java, but I have a problem understanding the Primitive Types in Objective-C. 我需要将一段代码从Objective-C转换为Java,但是我在Objective-C中理解原始类型时遇到了问题。 So I had their data types in my Objective-C code : 所以我在Objective-C代码中有他们的数据类型:

UInt64, Uint32, UInt8 , UInt64, Uint32, UInt8UInt64, Uint32, UInt8

which are unsigned integers (as I understand from internet). 这是无符号整数(据我所知,从互联网)。 So my question is, can I use Java primitive types like byte (8bit) - instead of UInt8 , int (32bit) - instead of UInt32 , and long (64bit) - instead of UInt64 . 所以我的问题是,我可以使用Java基本类型,如byte (8位) -而不是UInt8int (32位) -而不是UInt32 ,和long (64位) -而不是UInt64

Unfortunately, it isn't a straight translation and without knowing more about your program, its hard to suggest what the "right" approach is. 不幸的是,它不是一个简单的翻译,如果不了解你的程序,很难说明“正确”的方法是什么。

  • If your UInt8 values really range from 0-255, you may have to use Java signed int to be able to hold the entire range. 如果您的UInt8值确实在0-255之间,则可能必须使用Java signed int来保存整个范围。

  • If you are dealing with byte streams or memory layouts and really need to use just a single byte of memory, than you could try byte , but you may have to test and handle cases to handle when the high-bit is set (value > 127). 如果您正在处理字节流或内存布局,并且确实只需要使用单个字节的内存,那么您可以尝试使用byte ,但是您可能必须测试并处理设置高位时要处理的情况(值> 127 )。 Ditto with the other unsigned types. 与其他无符号类型同上。

Ideally, if your code just kind of "defaulted" to the unsigned types, but really the signed versions would have worked fine too (ie the ranges of your values never equal or exceed 2^7, 2^15, or 2^31 respectively), then you may be fine with the "straight" translation to byte , int , and long . 理想情况下,如果您的代码只是对无符号类型“默认”,但实际上签名版本也可以正常工作(即您的值的范围永远不会等于或超过2 ^ 7,2 ^ 15或2 ^ 31 ),那么你可以很好地将“直接”转换为byteintlong

Yes, those are the correctly sized data types to use in Java. 是的,这些是在Java中使用的正确大小的数据类型。 Make sure you take into account that Java does not have unsigned types and the trick is to use the next largest size. 确保考虑到Java没有无符号类型 ,并且诀窍是使用下一个最大的大小。 64 bit unsigned arithmetic requires special consideration. 64位无符号算术需要特别考虑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM