#include <iostream>
#include <string>
#include <math.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n = 0;
cin >> n;
string* Hex = new string[n];
string tmpOct;
string* Oct = new string[n];
for (int i = 0; i<n; i++)
{
int CurBit = 0;
cin >> Hex[i];
tmpOct = "";
Oct[i] = "";
for (int j = Hex[i].size() - 3; j >= 0; j -= 3)
{
int d = 0;
for (int k = 0; k<3; k++)
{
int t = j + k;
// 16 To 10
if (Hex[i][t] >= '0' && Hex[i][t] <= '9')
{
CurBit = Hex[i][t] - '0';
}
if (Hex[i][t] >= 'A' && Hex[i][t] <= 'F')
{
CurBit = Hex[i][t] - 'A' + 10;
}
d = d * 16 + CurBit;
}
// 3bit hex to 4bit oct
int base = 7; // 111B
for (int k = 0; k<4; k++)
{
tmpOct += (char)('0' + (d & base));
d = d >> 3;
}
d = 0;
}
// last less three
int rest = Hex[i].size() % 3;
if (rest != 0)
{
int d = 0;
for (int k = 0; k<rest; k++)
{
// 16 To 10
if (Hex[i][k] >= '0' && Hex[i][k] <= '9')
{
CurBit = Hex[i][k] - '0';
}
if (Hex[i][k] >= 'A' && Hex[i][k] <= 'F')
{
CurBit = Hex[i][k] - 'A' + 10;
}
d = d * 16 + CurBit;
}
int base = 7; // 111B
int max = ceil(4.0 / 3.0 * rest);
// 1bit hex = 4/3 bit oct
for (int k = 0; k<max; k++)
{
if (((k == max - 1) && (d & base) != 0) || k<max - 1)
tmpOct += char('0' + (d & base));
d = d >> 3;
}
}
int j = tmpOct.size() - 1;
// turn order
for (; j >= 0; j--)
{
Oct[i] += tmpOct[j];
}
}
for (int i = 0; i<n; i++)
{
cout << Oct[i] << endl;
}
cin >> n;
return 0;
}
蓝桥杯:十六进制转八进制算法
最新推荐文章于 2023-11-23 22:27:08 发布