Hello.

You must write a program that performs this translation. In C language. I started writing, but something is not being done.

#include <stdio.h> #include <string.h> #include <ctype.h> #include <math.h> #include <stdio.h> 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", &s ") ; return 0; } void main() { char st[10]; char ch; scanf (" % s ", &st "); printf("Rezultant: n"); HEX_TO_DEC(st); } 

Where is the mistake? Thank you in advance.



    6 answers 6

    Wrong:

     printf( "%s", &s"); 

    First, you even have a Hashcode that highlights the quotes as they are, secondly, s is a number, and output as a string. Try this:

     printf( "%d", s); 

    Well, in scanf, remove the extra quotes.

    Threat Hint. Symbols from A to F and 0 to 9 go in order, your switch is not necessary, at least it can be reduced by 8 times.

    upd here is a compiled version

      Tried to fix your code with a minimum of changes:

      1. In printf and scanf extra quotation mark before the closing bracket
      2. In printf pass the value, not the address (extra ampersand)
      3. In scanf format string is written incorrectly, instead of " % s " write "%s" , leaving a space before the modifier, you require that the space appears when you enter, since it does not exist, scanf believes that the parsing failed and no assignments will be made , then the translation function will work with the garbage from the stack.
      4. (Optional). The ch variable in the main function is not used, you can delete it.
      5. (Optional). I think before outputting the result, you wanted to print "Rezultant:\n" instead of "Rezultant: n"
      6. (Optional). You connect stdio.h twice.

      Since the input hexadecimal number is assumed to be short (the value breaks into int), I would use the strtol() function (without forgetting to include the header file stdlib.h ):

       char st[10]; long result; scanf ("%9s", &st); result = strtol(st, NULL, 16); printf("Rezultant: %ld\n", result); 

      If necessary, you can always verify the correctness of the input data using the second parameter strtol() . For simplicity, in this example, I do not use it.

      • IMHO, this is an educational task for writing the strtol function. it is clear that everything is already written before us ... - Yura Ivanov
      • 2
        Along with strtol (), you can use sscanf (st, "% x", & s); sscanf () returns the number of successfully disassembled (entered, read) elements, which allows you to analyze input errors, although strtol () is a more flexible function in this regard. I will add that both strtol () and sscanf () normally recognize the input in both 123 and 0x123 formats (sometimes this is convenient). - avp

      And if so? (slightly corrected). Only translates not from hexadecimal to decimal, but reads from hexadecimal to integer unsigned

       #include <stdio.h> #include <ctype.h> 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 = toupper(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 << 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", &d); printf("Compare to %u\n", d); } 

        Are you sure that this is correct:

        printf("%s, &s") ;

        ?

        • It is not right. Fixed, but still does not work ( - Alexander Romashkevich

        You, probably, here: transfer from one number system to another .

          In order not to be confused with types, you can use the library iostream.h, which, in my opinion, is more convenient than stdio.h.

          In it (example):

          • Input: cin>>a; // a - variable
          • Conclusion: cout<<"Переменная a - "<<a;

          Variable types do not need to be specified.

          • 2
            And now look at the language of the question. - VioLet