Home > Back-end >  Turn for help C binary decimal fault
Turn for help C binary decimal fault

Time:06-11

Title description

Given a binary representation of the nonnegative integers, converts it to a hexadecimal number,

Enter

How much input data set, the first data set number T, the T group data,
Each set of data a line, each behavior a binary integer (not including redundant leading 0), up to 1000,

O

Output corresponding hexadecimal number, in capital letters, said




#include
using namespace std;
Int main ()
{
Int n, I, len, j, k, sum, c, u.
Char d [1001];
Cin> n;
for(i=1; i<=n; I++)
{
The fflush (stdin);
Gets (d);
Len=strlen (d);
C=len % 4;
If (c!=0)
{
For (j=len + 3 - c; J>=4 - c; J -)
{
D=d [j] [j - (4 - c)];
}
for(j=0; j<4 - c; J++)
{
D [j]='0';
}
}
Len=strlen (d);
for(j=1; j<=len/4; J++)
{
sum=0;
For (u=1; U<=4; U++)
{
[sum +=(int) (d (j - 1) * 4 + u - 1] - 48) * pow (2, 4 - u);
If (u==4)
{
If (sum<=9)
{
Printf (" % d ", sum);
}
The else
{
The switch (sum)
{
Case 10: printf (" % c ", 'A');
break;
Case 11: printf (" % c ", "B");
break;
Case 12: printf (" % c ", "c");
break;
Case 13: printf (" % c ", "D");
break;
Case 14: printf (" % c ", 'E');
break;
Case 15: printf (" % c ", "F");
break;
}
}
}

}

}
printf("\n");
}
return 0;
}

CodePudding user response:

Well, you write is too messy, and how the circle hasn't exit turns ABCDEF? Might even consider not cycle

CodePudding user response:

Too complicated,

Read to a character array,
Two four hexadecimal said one hexadecimal, from the backward calculation,
ABCDEF directly by ASCII,


CSDN BBS newbie guide

CodePudding user response:


I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10581430.html
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10768339.html
  • Related