十六进制转八进制

十六进制转八进制

#include<iostream>
using namespace std;
class chr
{
    public:
    char data;
    chr *next;
    static long int scanf(chr *start)//返回长度
    {
        chr *h=start;
        chr *pre=NULL;
        char c;
        long int len=0;
        bool ok=false;
        while(1)
        {
            c=getc(stdin);
            if(!ok&&c=='\n')
                continue;
            if(c=='\n')
            {
                if(pre!=NULL)
                pre->next=NULL;
                delete h;
                break;
            }
            if(!ok)
             ok=true;
            len++;
            h->data=c;
            pre=h;
            h->next=new chr;
            h=h->next;
            h->next=NULL;
        }
        return len;
    }
    void release()
    {
        if(this->next==NULL)return;
        this->next->release();
        delete (this->next);
    }
};
unsigned char hextodecimal(unsigned char y)
{
    if(y>='A'&&y<='F')
        return (y-'A'+10);
    else if(y>='a'&&y<='f')
        return (y-'a'+10);
    else if(y>='0'&&y<='9')
        return y-'0';
    return 0;
}
void otc(chr **str,int strlens,char *out,int *len2,bool show=true)
{
    if(strlens==0)
    return;
    int x=0;
    for(int i=0;i<strlens;i++)
    {
        x<<=4;
        unsigned char s=hextodecimal((unsigned char)(*str)->data);
        *str=(*str)->next;
        x|=s;
    }
    bool ok=false;
    for(int i=0;i<4;i++)
    {
        unsigned char tt=((x>>(12-((i+1)*3)))&7);
        if(!ok)
        {
            if(tt==0)
            continue;
        }
        if(!ok&&tt!=0)
            ok=true;
        if(show)
            printf("%d",tt);
        else
        {
            out[*len2]=(char)(tt+'0');
            *len2+=1;
        }
    }
}
int main()
{
    //char us[133334];
    chr *ts=new chr,*tmp;
    int c;
    cin>>c;
    for(int i=0;i<c;i++)
    {
        long int len=chr::scanf(ts);
        long int n=len/3;
        int m=(int)(len%3);
        int y=0;
        tmp=ts;
        otc(&tmp,m,NULL,NULL);
        for(long int j=0;j<n;j++)
            otc(&tmp,3,NULL,NULL);
        //us[y]=0;
        ts->release();
        //cout<<us;
        printf("\n");
    }
    delete ts;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值