Objective C Convert int to NSString (iPhone)

int, iphone, objective-c, string

Solution

You have to use `%@` as the conversion specifier for an NSString. Change your last line to:

NSLog(@"%@:%@:%@", hoursStr, minsStr, secsStr);

`%a` means something totally different. From the `printf()` man page:

aA The double argument is rounded and converted to hexadecimal notation in the style

[-]0xh.hhhp[+-]d

where the number of digits after the hexadecimal-point character is equal to the precision specification.

Problem

I have the following code that is meant to convert milliseconds into hours, mins and seconds: ``` int hours = floor(rawtime / 3600000); int mins = floor((rawtime % 3600000) / (1000 * 60)); int secs = floor(((rawtime % 3600000) % (1000 * 60)) / 1000); NSLog(@"%d:%d:%d", hours, mins, secs); NSString *hoursStr = [NSString stringWithFormat:@"%d", hours]; NSString *minsStr = [NSString stringWithFormat:@"%d", mins]; NSString *secsStr = [NSString stringWithFormat:@"%d", secs]; NSLog(@"%a:%a:%a", hoursStr, minsStr, secsStr); ``` Fairly straightforward. Rawtime is an int with value 1200. The output is like this: ``` 0:0:1 0x1.3eaf003d9573p-962:0x1.7bd2003d3ebp-981:-0x1.26197p-698 ``` Why is it that converting the ints to strings gives such wild numbers? I've tried using %i and %u and they made no difference. What is happening?

Original source

Related problems