How to convert decimal into octal in objective c

扶醉桌前 提交于 2019-12-25 18:56:36

问题


just like my question, How can i convert decimal into octal in objective c? can somebody help me? it's make me dizy


回答1:


You have to initialize a new NSString object using the right format specifier.

Like :

int i = 9;
NSString *octalString = [NSString stringWithFormat:@"%o", i]; // %O works too.

This will create an autoreleased NSString object containing octal string representation of i. In case you start from a NSString containing a decimal number representation, you should first retrieve the number using :

int i = [myDecimalNumberAsAString intValue];

Here is a link to the format specifiers reference.

Hope this helps.




回答2:


Since Objective C is a superset of C you can just use C functions such as sprintf, e.g.

char s[32];
int n = 42;
sprintf(s, "%o", n);


来源:https://stackoverflow.com/questions/6647787/how-to-convert-decimal-into-octal-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!