Hello.
It is necessary to write a program performing this translation. In C language. He began to write, but something is not satisfied.
# include & lt; stdio.h & gt;
#Include & lt; String.h & gt;
#include & lt; ctype.h & gt;
#Include & lt; math.h & gt;
#Include & lt; stdio.h & gt;
int HEX_TO_DEC (char st [10])
{
int i, s, k, p;
s = 0;
p = strlen (st) - 1;
for (i = 0;! st [i] = '\ 0'; i ++) {
switch (toupper (st [i])) {
case 'A':
k = 10;
Break;
case 'B':
k = 11;
Break;
case 'C':
k = 12;
Break;
case 'D':
k = 13;
Break;
case 'E':
k = 14;
Break;
case 'F':
k = 15;
Break;
case '1':
k = 1;
Break;
case '2':
k = 2;
Break;
case '3':
k = 3;
Break;
case '4':
k = 4;
Break;
case '5':
k = 5;
Break;
case '6':
k = 6;
Break;
case '7':
k = 7;
Break;
case '8':
k = 8;
Break;
case '9':
k = 9;
Break;
case '0':
k = 0;
Break;
}
s = s + k * pow (16, p);
P--;
}
printf ( "% s", & amp; s ");
Return 0;
}
Void Main ()
{
char st [10];
char ch;
scanf ( "% s", & amp; st ");
printf ( "Rezultant: n");
HEX_TO_DEC (st);
}
What is the error? Thanks in advance.
Answer 1, Authority 100%
Wrong:
printf ( "% s", & amp; s ");
First, you even Heshkod highlights that quotes both are horrible, secondly, s
– the number and output as a string. Try this:
printf ( "% d", s);
Well, in scanf remove the extra quotation marks.
PS Hint. Characters from A to F and 0 to 9 are in order, there is no need, at least it can be reduced by 8 times in your switch.
upd here compiled version
Answer 2, Authority 80%
I tried to fix your code with a minimum of changes to:
- The
printf
andscanf
superfluous quote before the closing parenthesis - The
printf
pass a value, and not the address (an extra ampersand) - The
scanf
format string is not written correctly, instead of"% s"
email"% s"
, leaving a space before the modifier, you need to a gap appeared when entering, because it is not,scanf
believes that the line parsing failed and no assignment will perform, then the transfer will work with the garbage from the stack. - (Optional). The variable
ch
function in themain
is not used, can be removed. - (Optional). I think before the result is displayed, you want to print the
"Rezultant: \ n"
instead of"Rezultant: n"
- (Optional). You connect the two
stdio.h
.
As the input means the hexadecimal number short (meaning breaks int), I would use the function strtol ()
(do not forget to include the header file stdlib.h
):
char st [10];
long result;
scanf ( "% 9s", & amp; st);
result = strtol (st, NULL, 16);
printf ( "Rezultant:% ld \ n", result);
If you want to always be able to check the correctness of the input data using a second parameter strtol ()
. For simplicity, in this example I do not use it.
Answer 3, Authority 20%
And if so? (Slightly corrected). Only translates not from hexadecimal to decimal, and hexadecimal to read from an unsigned integer
# include & lt; stdio.h & gt;
#Include & lt; ctype.h & gt;
Void Hex_TO_DEC (Char St [10])
{
int i, k;
unsigned int s = 0;
For (i = 0; ST [i]! = '\ 0'; I ++)
{
int c;
Switch (C = TUPPER (ST [I]))
{
Case 'A':
Case 'b':
Case 'C':
Case 'D':
Case 'E':
Case 'F':
k = c - 'a' + 10;
Break;
Case '1':
Case '2':
Case '3':
Case '4':
Case '5':
Case '6':
Case '7':
Case '8':
Case '9':
Case '0':
k = c - '0';
Break;
}
s = (s & lt; & lt; 4) + k;
}
Printf ("% u \ n", s);
}
Void Main ()
{
char st [10];
char ch;
unsigned int d;
scanf ("% 9s", ST);
Printf ("Rezultant:");
Hex_to_dec (ST);
SSCANF (ST, "% X", & amp; D);
PrintF ("Compare to% u \ n", d);
}
Answer 4
you are sure that this is right:
printf ("% s, & amp; s");
?
Answer 5
You probably here: Transfer from some number systems to other .
Answer 6
To not be confused with types, you can use the library iOSTREAM.H, which, in my opinion, is more convenient than stdio.h.
in it (example):
- Enter:
CIN & GT; & GT; A;
// A – Variable - output:
cout & lt; & lt; "variable a -" & lt; & lt; a;
Types of variables do not specify.