鱼C论坛

 找回密码
 立即注册
查看: 2167|回复: 3

十六进制转换为八进制怎么转换呢?

[复制链接]
发表于 2021-3-4 15:25:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
//利用switch+string类型
//注意字符串要双引号

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
using namespace std;
int main(){
    int n;
    string str_temp,str_in;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        str_in="";
        str_temp="";//给字符串赋空

        cin>>str_in;
        int len=str_in.length();

        for(int j=0;j<len;j++){
            switch(str_in[j]){
                case '0': str_temp+="0000";break;
                case '1': str_temp+="0001";break;
                case '2': str_temp+="0010";break;
                case '3': str_temp+="0011";break;
                case '4': str_temp+="0100";break;
                case '5': str_temp+="0101";break;
                case '6': str_temp+="0110";break;
                case '7': str_temp+="0111";break;
                case '8': str_temp+="1000";break;
                case '9': str_temp+="1001";break;
                case 'A': str_temp+="1010";break;
                case 'B': str_temp+="1011";break;
                case 'C': str_temp+="1100";break;
                case 'D': str_temp+="1101";break;
                case 'E': str_temp+="1110";break;
                case 'F': str_temp+="1111";break;
                default: break;
            }
        }
        //cout<<str_temp<<endl;
        //把01串转化成目标串
        int len_s=str_temp.length();
        int ans=2;
        int sum=0;
        int r=len*4%3;

        if(r==1){
            if(str_temp[0]=='1')
                printf("1");
        }
        else if(r==2){
            if(str_temp[0]=='0'&&str_temp[1]=='1')
                printf("1");
            else if(str_temp[0]=='1'&&str_temp[1]=='0')
                printf("2");
            else if(str_temp[0]=='1'&&str_temp[1]=='1')
                printf("3");
        }
        else{
            if(str_temp[0]=='0'&&str_temp[1]=='0'&&str_temp[2]=='0')
                r=3;
        }

        for(int i=r;i<len_s;i++)
            {
                if(str_temp[i]=='1')
                    sum+=pow(2,ans);
                ans--;
                if(ans==-1)
                {
                    printf("%d",sum);
                    ans=2;
                    sum=0;
                }
            }
        printf("\n");
    }
}
从绿色字体部分往下就不明白了 求大佬解答!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-4 15:26:50 | 显示全部楼层
第六十二行往下不明白 求大佬解答
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-4 17:37:59 | 显示全部楼层
本帖最后由 墙里秋千墙外荡 于 2021-3-4 17:40 编辑

//利用switch+string类型
//注意字符串要双引号

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
using namespace std;
int main(){
    int n;
    string str_temp,str_in;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        str_in="";
        str_temp="";//给字符串赋空

        cin>>str_in;
        int len=str_in.length();

        for(int j=0;j<len;j++){
            switch(str_in[j]){
                case '0': str_temp+="0000";break;
                case '1': str_temp+="0001";break;
                case '2': str_temp+="0010";break;
                case '3': str_temp+="0011";break;
                case '4': str_temp+="0100";break;
                case '5': str_temp+="0101";break;
                case '6': str_temp+="0110";break;
                case '7': str_temp+="0111";break;
                case '8': str_temp+="1000";break;
                case '9': str_temp+="1001";break;
                case 'A': str_temp+="1010";break;
                case 'B': str_temp+="1011";break;
                case 'C': str_temp+="1100";break;
                case 'D': str_temp+="1101";break;
                case 'E': str_temp+="1110";break;
                case 'F': str_temp+="1111";break;
                default: break;
            }
        }
        //cout<<str_temp<<endl;
        //把01串转化成目标串
        int len_s=str_temp.length();
        int ans=2;
        int sum=0;
        int r=len*4%3;

        if(r==1){
            if(str_temp[0]=='1')
                printf("1");
        }
        else if(r==2){
            if(str_temp[0]=='0'&&str_temp[1]=='1')
                printf("1");
            else if(str_temp[0]=='1'&&str_temp[1]=='0')
                printf("2");
            else if(str_temp[0]=='1'&&str_temp[1]=='1')
                printf("3");
        }
       else{//对3取余r=0的情况
            if(str_temp[0]=='0'&&str_temp[1]=='0'&&str_temp[2]=='0')//如果前三位都为0则跳过对最高三位的打印,毕竟打印一个0也没什么意义
                r=3;//因为跳过高三位的处理,故r=3
        }

        for(int i=r;i<len_s;i++)//该处则从第r位开始向后处理,第0位到第r-1位在前面已经处理过了
            {
                if(str_temp[i]=='1')
                    sum+=pow(2,ans);//如果该位为1则进行相应的幂处理,为0情况if判断为否跳过该步
                ans--;//对幂的次数递减,毕竟每3位都是高位在前
                if(ans==-1)//如果ans为-1,则说明该三位处理完了,对这三位二进制数进行一次八进制打印并重置ans和sum
                {
                    printf("%d",sum);
                    ans=2;
                    sum=0;
                }
            }
        printf("\n");
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-6 11:09:19 From FishC Mobile | 显示全部楼层
十六进制转八进制还需要先转二进制?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 11:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表