tags:

views:

47

answers:

2

Are there any methods in objective C for converting byte to int, float and NSString?

+3  A: 

The first are C-types. No conversion is needed, just assign them:

byte b = ...;

int x = b;
float f = b;

Converting to NSString could be done using stringWithFormat:, a NSNumberFormatter and many more methods. This is the easiest:

NSString *myString = [NSString stringWithFormat: @"%d", b];

If you want it printed in hex, use @"%x" (for lowercase letters) or @"%X" (for capital letters) instead.

Max Seelemann
What if I have an array of four byte and I want to convert it into int.
Ideveloper
bitshift and add them. Like so: `int x = (arr[0] << 12) + (arr[1] << 8) + (arr[2] << 4) + arr[3]`
Max Seelemann
That helped. Thanx alot
Ideveloper
A: 

You could get an NSString from NSData with initWithData:encoding: and then transform it into int or float by calling intValue and floatValue

rano