十六进制转八进制
Posted xuyiting
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了十六进制转八进制相关的知识,希望对你有一定的参考价值。
题目链接:http://lx.lanqiao.cn/problem.page?gpid=T51
demo:
#include <iostream> #include <string> using namespace std; int main() { int Number; cin >> Number; for(int k = 1; k <= Number; k++) { string String_Hex, String_Binary; cin >> String_Hex; String_Binary = ""; for(int i = 0;i < String_Hex.length(); i++){ switch(String_Hex[i]) { case ‘0‘:String_Binary += "0000"; break; case ‘1‘:String_Binary += "0001"; break; case ‘2‘:String_Binary += "0010"; break; case ‘3‘:String_Binary += "0011"; break; case ‘4‘:String_Binary += "0100"; break; case ‘5‘:String_Binary += "0101"; break; case ‘6‘:String_Binary += "0110"; break; case ‘7‘:String_Binary += "0111"; break; case ‘8‘:String_Binary += "1000"; break; case ‘9‘:String_Binary += "1001"; break; case ‘A‘:String_Binary += "1010"; break; case ‘B‘:String_Binary += "1011"; break; case ‘C‘:String_Binary += "1100"; break; case ‘D‘:String_Binary += "1101"; break; case ‘E‘:String_Binary += "1110"; break; case ‘F‘:String_Binary += "1111"; break; default:break; } } if(String_Binary.length() % 3 == 1) String_Binary = "00" + String_Binary; else if(String_Binary.length() % 3 == 2) String_Binary = ‘0‘ + String_Binary; int Sign_Leading_Zero = 0; for(int i = 0;i <= String_Binary.length() - 3; i += 3){ int Digit_Number = 4 * (String_Binary[i] - ‘0‘) + 2 * (String_Binary[i+1] - ‘0‘) + (String_Binary[i+2] - ‘0‘); if(Digit_Number) Sign_Leading_Zero = 1; if(Sign_Leading_Zero) cout << Digit_Number; } cout << endl; } return 0; }
以上是关于十六进制转八进制的主要内容,如果未能解决你的问题,请参考以下文章