如何在objective-c中将字节值转换为int

sus*_*use 3 c iphone byte integer objective-c

请告诉我如何在iPhone编程的objective-c中将字节转换为NSInteger/int?

Jer*_*myP 5

如果按字节,你的意思是一个无符号的 8 位值,下面会做。

uint8_t foo = 3;   // or unsigned char foo...
NSInteger bar = (NSInteger) foo;
Run Code Online (Sandbox Code Playgroud)

甚至

NSInteger bar = foo;
Run Code Online (Sandbox Code Playgroud)


Vla*_*mir 5

你对"字节"是什么意思?如果要将表示整数值的单字节转换为int(或NSInteger)类型,只需使用"=":

Byte b = 123;
NSInteger x;
x = b;
Run Code Online (Sandbox Code Playgroud)

as Byte(与unsigned char - 1字节无符号整数相同)和NSInteger(与int - 4字节有符号整数相同)都是简单整数类型,可以自动转换.您应该阅读有关" c数据类型 "和" 转换规则 "的更多信息.例如http://www.exforsys.com/tutorials/c-language/c-programming-language-data-types.html

如果要将存储某些值的多个字节转换为int,则convertion取决于这些数据的结构:每个值有多少字节,有符号或无符号.

  • 这是一个客观问题.NSInteger在NSObjCRuntime.h中定义,NSObjCRuntime.h中的第一个#import是<objc/objc.h>,所以我认为objective-c标记没问题.(只是我的猜测),这个问题背后的基本原理是:对于Objective-c的新手可能看不到NSInteger和NSNumber之间的区别. (2认同)