Base conversion - from hexadecimal octal

Hexadecimal number is converted to octal

Title Description

Given a positive integer n hexadecimal, octal output thereof corresponds.

输入格式
  The first act input a positive integer n (1 <= n <= 10).
  Next n lines, each line a from 0 to 9, the uppercase A string consisting of F, converted to hexadecimal represents a positive integer, each hexadecimal number length does not exceed 100,000.

输出格式
  N output lines, each act input octal positive integer corresponding.

Topic analysis

Converted by bit hexadecimal 4-bit binary, then a set of three, to convert octal
Note: If the string concatenation and taken by way of suitable output is intended to meet the problem

Code

完整代码

#include<iostream>
#include<string>
#include <stdio.h>
#include <stdlib.h>
#include<sstream>//用于实现string >> int 
#include<cmath>//数学函数
#include <Windows.h>
using namespace std;

//首位填充0元素 
string MakeUp(string str,int num){
	string zero;
	int len=str.length();
	int n;
	if(len<num){
		n=num-len;
		for(int i=0;i<n;i++)
		zero+="0";
		return zero+str;
	}
	else 
		return str;
}

//2进制转8进制 
string BitToOct(string str){
	string oct;//8进制字符串接收返回值 
	string new_oct;
	string bit;//存储截取的3位2进制字符串
	stringstream ss;//采用文件流将string 与 int 相互转换
	while(str.length()!=0){
		//判断是否够3位
		if(str.length()<3){
			str=MakeUp(str,3);//补0,位数是3位
			int sum=0,a;
			//按权值相加,得到8进制数
			for(int i=0;i<3;i++){
				stringstream s1;
				s1<<str[i]; 
				s1>>a;
				if(a)
					sum+=pow(2.0,2-i);
			}
			str="\0";//字符串清空 
			ss<<sum;
			ss>>oct;
			ss.clear();//务必清除,否则会出错
			new_oct+=oct;//字符串拼接

		}else{
			bit=str.substr(str.length()-3);//截取后三位
			int sum=0,a;
			//按权值相加,得到8进制数
			for(int i=0;i<3;i++){
				stringstream s1;
				s1<<bit[i]; 
				s1>>a;
				if(a)
					sum+=pow(2.0,2-i);
			}
			
			str=str.substr(0,str.length()-3);//删除后三位
			
			ss<<sum;
			ss>>oct;
			ss.clear(); //务必清除,否则会出错
			new_oct+=oct;//字符串拼接
			
		}
	} 

	return new_oct;
	
}


//16进制转换2进制,此处注意:不足4位补齐四位 
string HexToBit(char c){
	string str;//接收返回值 
	int a=0;//十进制数 
	char cc[4];
	if(c>='0'&&c<='9')
		a=a+c-'0';
	else if(c>='A'&&c<='F')
		a=a+c-'A'+10;
	itoa(a,cc,2);//将10进制数转换为2进制数
	str=cc;//所得存入字符串 	
	str=MakeUp(str,4);//补0,位数是4位
	return str;
}

//倒置输出
string IndOutput(string str){
	string oct;
	int len=str.length();
	for(int i=len-1;i>=0;i--){
		if(i==len-1&&str[i]=='0'){	//抹去0操作 
			oct+="";
		}else{
			oct+=str[i];//字符串拼接
		}
	}
	return oct;
}


int main(){
	int n;//数字个数
	string hex[10];
	string bit;//存放2进制位 
	cin>>n;
	//循环输入
	for(int i=0;i<n;i++)
		cin>>hex[i];
	
	for(int i=0;i<n;i++){
		int len=hex[i].length();
		for(int j=0;j<len;j++){
			string str=hex[i].substr(j,1);//截取每一位
			char c=str[0];
			bit+=HexToBit(c);
		}
		
		cout<<IndOutput(BitToOct(bit))<<endl;//逆序输出,
		bit="\0";//bit清空 
	}
	
}

精简代码

#include <iostream>
#include <string>  
using namespace std;  

int main()  
{  
   int n = 0, i = 0, j = 0, temp = 0, nNum = 0;  
   char ch;  
   string strHex[10];  
   string strBin[10];  
   string strOct[10];  

   cin>>n;  
   for (i = 0; i < n; ++i)  
   {  
       cin>>strHex[i];  
   }  

   //十六进制转二进制  
   for (i = 0; i < n; ++i)  
   {  
       j = 0;  
       while (strHex[i][j])  
       {  
           switch(strHex[i][j])  
           {  
           case '0': strBin[i] += "0000"; break;  
           case '1': strBin[i] += "0001"; break;  
           case '2': strBin[i] += "0010"; break;  
           case '3': strBin[i] += "0011"; break;  
           case '4': strBin[i] += "0100"; break;  
           case '5': strBin[i] += "0101"; break;  
           case '6': strBin[i] += "0110"; break;  
           case '7': strBin[i] += "0111"; break;  
           case '8': strBin[i] += "1000"; break;  
           case '9': strBin[i] += "1001"; break;  
           case 'A': strBin[i] += "1010"; break;  
           case 'B': strBin[i] += "1011"; break;  
           case 'C': strBin[i] += "1100"; break;  
           case 'D': strBin[i] += "1101"; break;  
           case 'E': strBin[i] += "1110"; break;  
           case 'F': strBin[i] += "1111"; break;  
           default:break;  
           }  
           ++j;  
       }  
   }  

   //二进制转化为八进制  
   for (i = 0; i < n; ++i)  
   {  
       j = strBin[i].size()-1;//获得长度  
       while (strBin[i][j] && j>=0)  
       {  
           temp = 3;  
           nNum = 0;  
           while (temp-- && j>=0)  
           {  
               if ('1' == strBin[i][j])  
               {  
                   switch(temp)  
                   {  
                   case 0: nNum += 4; break;  
                   case 1: nNum += 2; break;  
                   case 2: nNum += 1; break;  
                   default:break;  
                   }  
               }  
               --j;  
           }  
           strOct[i] += (nNum+'0');  
       }  
   }  

   //字符串逆序  
   for (i = 0; i < n; ++i)  
   {  
       temp = strOct[i].size()-1;  
       for (j = 0; j <= temp/2; ++j)  
       {  
           ch = strOct[i][j];  
           strOct[i][j] = strOct[i][temp-j];  
           strOct[i][temp-j] = ch;  
       }  
   }  
   //打印  
   for (i = 0; i < n; ++i)  
   {  
       j = 0;  
       while (strOct[i][j++] == '0');//跳过前面的0  

       for(--j; j < strOct[i].size(); ++j)  
       {  
           cout<<strOct[i][j]-'0';  
       }  
       /*if (i != n-1)*/  
           cout<<endl;  
   }  

   return 0;  
}

operation result

2
76
941FAB
166
45017653
--------------------------------
Process exited after 12.85 seconds with return value 0

to sum up

About the hexadecimal conversions, character conversion mosaic is particularly important, but they first have to be converted to binary, and 16 >> 2, one becomes four, >> 2, 3 becomes one, about which not enough bits the first item taken up mode 0, which is the need to take into account. In short this is not the best base for the conversion, the next time to improve, can exhausted me.

Published 59 original articles · won praise 5 · Views 5063

Guess you like

Origin blog.csdn.net/qq_38496329/article/details/103599581