Home > Back-end >  Hexadecimal conversion, array
Hexadecimal conversion, array

Time:11-12

With an array of hexadecimal number (long), now want to convert it to a decimal output,
Plastic variable is not long enough, how to put new decimal number in the array output?

CodePudding user response:

Can refine the demand again, the hexadecimal array is what kind of? If it is an array, then an element of an element, if it is a big number, starting from its lowest, in turn * 16 ^ n, n according to different values for different digits,

CodePudding user response:

Such as char a []="999876543210" (first without letters, save)
Then I want to have it converted to a decimal number output,
For example, I int sum;
At this time with the increase of operation digits, numerical will overflow,

CodePudding user response:

I know how to convert hexadecimal into decimal, but for the digits can't do too much,,,

CodePudding user response:

 # include & lt; stdio.h> 
#include
#include

# define MAX_NUM_LEN 2048

Int cal_factorial (unsigned char * num_list, int num_size, int n);

Int cal_val (unsigned char * num_list, int num_size, int base, int num)
{
Int I=0, j;
Int len=1, TMP, carry_bit;

Num_list [1]=1;

While (I & lt; Num) {
Carry_bit=0;
For (j=1; J & lt;=len; J++) {
TMP=num_list [j] * base + carry_bit;
Num_list [j]=TMP % 10;
Carry_bit=TMP/10;
If (j & gt;=len & amp; & Carry_bit!=0)
len++;
If (num_size 1 & lt; Overflow len) {/* */
Printf (" Overflow! \n");
Return num_size - 1;
}
}
i++;
}

Return len.
}


Int main (void)
{
Unsigned char num_list [MAX_NUM_LEN];
Int num_len, I;
Int base, power;

Memset (num_list, 0, sizeof (num_list));
Printf (" both Please input two Numbers: ");
The scanf (" % d % d ", & amp; Base, & amp; Power);

If (base & lt; 0 & amp; & Power % 2)
Num_list [0]=1;/* negative number */
The else
Num_list [0]=0;
The base=abs (base);

Num_len=cal_val (num_list MAX_NUM_LEN, base, power).
Printf (" len=% d \ n ", num_len);
If (num_list [0])
Putchar (' - ');
For (I=num_len; I & gt;=1; I -)
Printf (" % d ", num_list [I]);
printf("\n");
return 0;
}


Int cal_factorial (unsigned char * num_list, int num_size, int n)
{
Int carry_bit, I, j, len=1;
Int TMP.

Num_list [1]=1;
For (I=2; I & lt;=n; I++)
{
Carry_bit=0;
For (j=1; J & lt;=len; J++) {
TMP=num_list [j] * I + carry_bit;
Num_list [j]=TMP % 10;
Carry_bit=TMP/10;
If (j & gt;=len & amp; & Carry_bit!=0)
len++;
If (num_size 1 & lt; Overflow len) {/* */
Printf (" Overflow! \n");
Return num_size - 1;
}
}

}

/*
For (I=len; I & gt;=1; I -)//reverse output array is the
Printf (" % d ", a [I]);
printf("\n");
*/
Return len.
}

For your reference ~

CodePudding user response:

This is the topic:

Hexadecimal conversion decimal
Enter a string (the length of the string does not exceed 20), to do the following treatment: filter after all the non hexadecimal character, form a new string (hexadecimal form), then it is converted to a decimal number after output,


# include
Int main (void)
{
Char input [21]={};
Char after [21]={};
Char hex [17]="0123456789 abcdef";
Double sum=0;
Int I, j, 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]=input [I] - 32;
}
}

for (i=0; i<20; I++)//determine whether for hexadecimal notation
{
for(j=0; J<16. J++)
{
If (input [I]==hex [j])
{
After [k]=input [I];//assigned to new string
k++;
}
}
}

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

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

for(i=0; i{
Sum=(sum + after [I]) * 16;
}
The sum +=after [I];

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

Input characters in a not long...