我知道通常从oct_to_dec转换。一些更聪明的方法?
答案 0 :(得分:1)
通过位掩码知道你想要做什么会有所帮助,因为有时候有更好的方法可以全局解决你的问题,而不是这个小的请求。
我有一种感觉,这是为了做作业,因为谷歌搜索这个问题发现我的论坛与家庭作业相同的查询。如果这是作业,那么请将其标记为作业,就像您最近提出的另一个问题一样。
感谢Google,我找到了this site 也许它会帮助你理解......
void convertBase(int decimal) //Function that convert decimal to base of 8
{
const int mask1 = (7 << 3);
const int mask2 = (7 << 0);
firstDigit = (decimal & mask1) + '0';
secondDigit = (decimal & mask2) + '0';
printf("Octal Representation of Binary Number: %d%d\n", firstDigit, secondDigit);
}
答案 1 :(得分:0)
此函数读取八进制字符串并返回其数值。
int parse_octal(const char* s) {
int r = 0;
for ( ; *s; s++)
r = (r << 3) | (*s & 7);
return r;
}
它使用位掩码来提取ASCII值的相关位。