Please tell me how to convert bytes to NSInteger/int in objective-c in iPhone programming?
-
What do you mean exactly by convert bytes to int?Jason Coco– Jason Coco2010-04-27 04:43:38 +00:00Commented Apr 27, 2010 at 4:43
-
'bytes'? What kind of bytes? Data? string? something over the net? xml? huh?bbum– bbum2010-04-27 04:47:40 +00:00Commented Apr 27, 2010 at 4:47
-
1@bbum, the "value" does not come with an "s" in the question title.ohho– ohho2010-04-27 04:52:31 +00:00Commented Apr 27, 2010 at 4:52
5 Answers
What do you mean by "Bytes"? If you want convert single byte representing integer value to int (or NSInteger) type, just use "=":
Byte b = 123;
NSInteger x;
x = b;
as Byte (the same as unsigned char - 1 byte unsigned integer) and NSInteger (the same as int - 4 bytes signed integer) are both of simple integer types and can be converted automatically. Your should read more about "c data types" and "conversion rules". for example http://www.exforsys.com/tutorials/c-language/c-programming-language-data-types.html
If you want to convert several bytes storing some value to int, then convertion depends on structure of these data: how many bytes per value, signed or unsigned.
2 Comments
If by byte, you mean an unsigned 8 bit value, the following will do.
uint8_t foo = 3; // or unsigned char foo...
NSInteger bar = (NSInteger) foo;
or even
NSInteger bar = foo;
1 Comment
NSInteger x = 3;
unsigned char y = x;
int z = x + y;
Use the "=" operator.