在Objective-C中将八进制字符串转换为十进制? [英] Converting a octal String to Decimal in Objective-C?

查看:105
本文介绍了在Objective-C中将八进制字符串转换为十进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Objective-C中进行二进制,八进制,十进制和十六进制之间的转换。
我无法将八进制转换为十进制。

I trying to do conversions between Binary, Octal, Decimal and Hexadecimal in Objective-C. I had problems converting Octal to Decimal.

我尝试了以下操作:

NSString *decString = [NSString stringWithFormat:@"%d", 077];

工作正常,返回预期的63,但是我的八进制值是NSString。我怎么能告诉计算机它是一个八进制;

It works fine, returning 63 as expected, but my Octal value is a NSString. How can I tell the computer that it is a Octal;

我知道有一个叫做 scanHexInt:的方法,我曾经将十六进制转换为十进制,但是似乎没有scanOctInt ...

I know there is a method called "scanHexInt:" which I used to convert Hexadecimal to decimal, but it seems there is no scanOctInt...

任何帮助将不胜感激!

推荐答案

最干净的解决方案可能是:

The cleanest solution is probably:

long result = strtol(input.UTF8String, NULL, 8);

long long result = strtoll(input.UTF8String, NULL, 8);

这篇关于在Objective-C中将八进制字符串转换为十进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆