开发者

how to convert an unsigned int to NSString?

开发者 https://www.devze.com 2023-03-14 01:37 出处:网络
I have an unsinged int having value of -10, now I want to convert it into NSString. In simply we do as, in obj开发者_运维问答ective-c, iphone programming

I have an unsinged int having value of -10, now I want to convert it into NSString.

In simply we do as, in obj开发者_运维问答ective-c, iphone programming

int x = 10;
NSString *str = [NSString stringWithFormat:@"%d",x];
NSLog(@"My String is %@",str);

I will show console output as 10

but if it is something like

unsigned int x = -10;
NSString *str = [NSString stringWithFormat:@"%d",x];
NSLog(@"My String is %@",str);

Then the output will be same, not the -10,

how can I do proper conversion ?


Did you ever ask yourself what unsigned means?

It is always >= 0. -10 is not a valid value for an unsigned variable.


Use %u instead of %d. It is similar to C : Convert signed to unsigned.


An unsigned integer can't have a value of -10.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号