十六进制转换八进制(普通版)

#include<iostream>
#include<string>


using namespace std;


string X_T(char value);
string T_E(string value);
string getTwo(string data);
string getEight(string data);


int main(int argc,char** argv){



string data;
cin >> data;
cout << getEight(getTwo(data)); 

return 0;
}


string getTwo(string data){

string resault = "";
int length = data.length();

for(int i = 0;i < length;i++){

resault = resault + X_T(data[i]);
}

return resault;
}


string getEight(string data){

string value = "";
string value_temp = "";
int length_ = data.length();
int length;

if(length_ % 3 == 0){

length = length_ / 3;

}else{

if(length_ % 3 == 1){

data = "00" + data;

}else if(length_ % 3 == 2){

data = "0" + data;

}

length_ = data.length();
length = length_ / 3;
}

for(int i = 0;i < length;i++){

value_temp = "";
value_temp.append(1,data[3 * i]).append(1,data[3 * i + 1]).append(1,data[3 * i + 2]);
value = value + T_E(value_temp);
}


int sign = 0;
string temp = "";

while(value[sign] == '0'){

sign++;
}

for(int i = sign;i < value.length();i++){

temp.append(1,value[i]);
}

return temp;
}


string T_E(string value){

if(value == "000"){

return "0";

}else if(value == "001"){

return "1";

}else if(value == "010"){

return "2";

}else if(value == "011"){

return "3";

}else if(value == "100"){

return "4";

}else if(value == "101"){

return "5";

}else if(value == "110"){

return "6";

}else if(value == "111"){

return "7";

}else{

return "";
}

}


string X_T(char value){

switch(value){

case '0':
return "0000";
break;

case '1':
return "0001";
break;

case '2':
return "0010";
break;

case '3':
return "0011";
break;

case '4':
return "0100";
break;

case '5':
return "0101";
break;

case '6':
return "0110";
break;

case '7':
return "0111";
break;

case '8':
return "1000";
break;

case '9':
return "1001";
break;

case 'A':
return "1010";
break;

case 'B':
return "1011";
break;

case 'C':
return "1100";
break;

case 'D':
return "1101";
break;

case 'E':
return "1110";
break;

case 'F':
return "1111";
break;
}
}
posted @ 2018-05-18 10:40  IT蓝月  阅读(587)  评论(0编辑  收藏  举报
Live2D