Home > Back-end >  How to turn a long of hexadecimal Numbers into decimal output?
How to turn a long of hexadecimal Numbers into decimal output?

Time:11-30

If the input is a long string of words in the last step will overflow the range of int, what method can not overflow? (just can output the result right)

The code is as follows:
# include
# include

Int main (void)
{
Char input [21]={}, after [21]={};
Char hex [17]="0123456789 abcdef";
Int then [21]={}, final [40]={};
int sum=0;
Int I, k=0;

Printf (" input a string length no more than 20: ");
Gets (input);

for (i=0; i<20; I++)//all lowercase variable capital
{
If (input [I] & gt;='a' & amp; & Input [I] <='z')
{
Input [I] -=32;
}
}

for (i=0; i<20; I++)//determine whether for hexadecimal notation
{
for(int j=0; J<16. J++)
{
If (input [I]==hex [j])
{
After [k]=input [I];//assigned to new string
k++;//to switch to the next, conveniently record the length of the string
}
}
}

Printf (" \ n hex: 0 x % s \ n ", after);

for(i=0; i{
If (after [I] & gt; '9')
Then [I]=after [I] - 'A' + 10;
The else
Then [I]=after [I] - '0';
}

for(i=0; i{
Sum=(sum + then [I]) * 16;//the results
}
The sum +=then [I];

printf("%d",sum);
return 0;
}

CodePudding user response:

High precision algorithm can solve the problem,
Said understand point is to use string (string or an array of char) to store the output,
  • Related