回顾二进制到16进制的方法
可以直接做
也可以从二转到10,,10转到16
看似容易
实则很需要细心,,
如何摆弄下标位置,如何计算边界,如何存储结果
#include <fstream>
#include <string.h>
#include <cstdio>
using namespace std;
#define _cdebbug
//全局数据
int main()
{
//重定向
#ifdef _cdebbug
freopen("F:\\input.txt","r",stdin );
#endif
char shuzi[12005];
char resault[3000];
int n;
int i,j;
int len;
int len16;
int k = 0;
int temp;
scanf("%d",&n);
while (n--)
{
scanf("%s",shuzi);
len = strlen(shuzi); //二进制数长度
len16 = (len + 3) / 4; //16进制数长度
resault[len16] = '\0';
for (i = len - 1; i >= 0; i -= 4)
{
temp = 0;
for (j = i - 3; j <= i; ++j)
{
if (j < 0)
j = 0;
temp *= 2;
temp += shuzi[j] -'0';
}
if (temp <= 9 && temp >= 0)
resault[--len16] = temp + '0';
else
resault[--len16] = temp - 10 + 'A';
}
printf("%s\n",resault);
}
//解除重定向关闭文件
#ifdef _cdebbug
fclose(stdin);
#endif
return 0 ;
}