How to Convert Nsinteger to Nsstring Datatype

How do I convert NSInteger to NSString datatype?

NSIntegers are not objects, you cast them to long, in order to match the current 64-bit architectures' definition:

NSString *inStr = [NSString stringWithFormat: @"%ld", (long)month];

Convert a NSInteger to NSString Xcode iOS8

intValue is not methods that exist on a NSInteger but on NSNumber.
Since NSInteger is a typedef for a primitive type int on 32Bit and long on 64bit it does not have an methods.

Here is how you'd could do it, casting to make sure it works on both 32 and 64 bit devices.

NSString *inStr = [NSString stringWithFormat:@"%ld", (long)clasesDadas ]; 

Also as stated by meronix the count method will result in an NSUInteger so you might want to check the type and format accordingly

how to convert NSinteger to String]

Try this:

NSMutableString *myWord = [[NSMutableString alloc] init];
for (int i=0; i<=200; i=i+10) {
[myWord appendString:[NSString stringWithFormat:@"%d", i]];
//...
}
//do something with myWord...
[myWord release];

NSInteger is simply a typedef to the int or long data types on 32/64-bit systems.

Converting NSInteger and NSString into array of bytes

Since your string is a UUID string you can do something like this:

NSString *test = @"";
uuid_t uuid;
uuid_parse([test UTF8String], uuid)
NSData *data = [NSData dataWithBytes:uuid length:16];

For the number you can do:

NSInteger test = 1;
NSData *data = [NSData dataWithBytes:&test length:sizeof(test)];

Keep in mind that NSInteger is probably more than two bytes and you may also need to worry about byte order.

Update: Since it seems you need the integer value to be two bytes, you should do:

uint16_t test = 1;
NSData *data = [NSData dataWithBytes:&test length:sizeof(test)];

This will ensure 2 bytes. You also need to worry about byte ordering so you really need:

uint16_t test = 1;
uint16_t bytes = CFSwapInt16HostToBig(test);
NSData *data = [NSData dataWithBytes:&bytes length:sizeof(bytes)];

Change CFSwapInt16HostToBig to CFSwapInt16HostToLitte if appropriate.

Convert any data type to NSString

  1. valueForKey: will never return int or float. Instead, it will wrap them into NSNumbers. Details here.

  2. Therefore, you can use "%@" to represent them in stringWithFormat:.

  3. You can also use description method of NSObject like this: [[contact valueForKey:formCell.variableName] description].

How can I cast an NSString as a hex representation of NSInteger

Turns out this is deliciously simple:

unsigned int bitshiftString;
[[NSScanner scannerWithString:flags] scanHexInt:&bitshiftString];

Transform NSInteger to NSString as a 5-digit number

Use the %05d format specifier in order to pad with zeroes:

NSString *myStr = [NSString stringWithFormat:@"%05d", myInt];

Wikipedia's explanation.



Related Topics



Leave a reply



Submit