在C++中实现对八进制字符串转换为整数的代码,如下:
#include <iostream>
inline bool IsOctNum(char c) { return !(c<'0'||c>'8'); }
inline int OctToDec(const char * oct_str) { int len=strlen(oct_str); int rtn=IsOctNum(oct_str[0])?oct_str[0]-'0':-1; for(int i=1;i<len&&rtn>-1;++i) if(IsOctNum(oct_str[i])) rtn=(rtn<<3)|(oct_str[i]-'0'); else break; return rtn; }
int main(int argc, char * argv[]) { char octs[]="0777"; std::cout<<OctToDec(argv[1])<<std::endl; return 0; |