16进制字符串转换为8进制

#include <iostream>
using namespace std;

void fun8(int x)
{
     if(x<8)
     {
            cout<<x;
     }
     else
     {
         fun8(x/8);
         cout<<x%8;
     }
}

int main(void)
{
    char ch[10];
    int result = 0;
   
    cin>>ch;
    for(int i=0;ch[i]!='\0';i++)
    {
            if(ch[i]>='0' && ch[i]<='9')
               result = result*16+ch[i]-'0';
            if(ch[i]>='a' && ch[i]<='f')
               result = result*16+ch[i]-'a'+10;
            if(ch[i]>='A' && ch[i]<='F')
               result = result*16+ch[i]-'A'+10;
    }
    fun8(result);
    return 0;
}

posted @ 2012-09-22 19:15  SA高处不胜寒  阅读(397)  评论(0编辑  收藏  举报