arrayscstring

converting hexadecimal string to decimal integer and vice-versa


Here the hexadecimal system is a bit different where A, B .... F are replaced by U-Z.

#include<stdio.h>

int main() {

    // Write your program logic here. This line is a comment, you can leave it in code if you wish.
    // conversion of hexadecimal to decimal.
    
    //1.taking user input of string
    char hexaDec[10];
    int counter = 0;
    for(int i = 0; i<10; i++){
        scanf("%c", &hexaDec[i]);
        if(i<=8){
            if(hexaDec[i] == '\n'){
                hexaDec[i+1] = '\0';
                break;
            }
        }
        counter++;
    }
    // hexaDec[10] = '\0'; wasn't sure if this was required
    
    int dnum = 0;
    scanf("%d", &dnum);
    
    int decimalC = 0;
    int product = 1;
    while(counter>0){
        char cnum = hexaDec[counter-1];
        
        int check = cnum - 'U';
        if(cnum - '0' >= 0 && cnum - '0' <= 9){
            decimalC = decimalC + (cnum - '0') * product;
        }
        else if(check >=0 && check <= 5) {
            decimalC = decimalC + (check + 10)*product;
        }
        
        product = product*16;
        counter--;
    
    }
    printf("%d\n", decimalC);
    
    // //decimal to hexadecimal
    
    char decimalH[10];
    int count = 0;
    if(dnum>0){
        for(count =0; dnum>0  && count<=10; count++){
            int rem = dnum%16;
            dnum = dnum/16;
            if(rem>=0 && rem<=9){
                decimalH[count] = rem +'0';
            }
            else if(rem>=10 && rem<=15){
                decimalH[count] = rem -10 + 'U';
            }
        }        
    }
    
    else if(dnum == 0){
        printf("%c", '0');
    }
    
    while(count>0){
        printf("%c", decimalH[count-1]);
        count--;
    }
    printf("%c", '\0');
    return 0;
}

I know the code is really long and can be simplified using functions but still I wanted to try it with primitive methods. It seems ok for all the problems still the college webpage is telling that I have failed a few cases. I have tried everything I could . Would really appreciate any help. Thank you in advance.


Solution

  • "taking user input of string" code is convoluted

    The need for if(i<=8) is a bit of of a mystery.
    decimalC is an odd name for a hexadecimal task.

    Decimal to hexadecimal deserves simplification

    Might as well use unsigned since sign characters are not processed.

    Add error checking


    Sample code that is something like OP's:

        // 1. Taking user input of string alternative
        #define INPUT_SIZE 10
        char hexaDec[INPUT_SIZE];
        int counter = 0;
        char ch;
    
        // Read the _entire_ line, regardless of its length.
        while (scanf("%c", &ch) == 1 && ch != '\n') {
          if (counter < INPUT_SIZE - 1) {
            hexaDec[counter++] = ch;
          } else {
            fprintf(stderr, "Too long\n");
          }
        }
        // Since the goal is "converting hexadecimal string",
        // append a null character to form a _string_.
        hexaDec[counter] = '\0';
    
    
        // Simplify the conversion by walking the string. 
        unsigned value = 0;
        for (int i = 0; hexaDec[i]; i++) {
          int digit;
          if (hexaDec[i] >= '0' && hexaDec[i] <= '9') {
            digit = hexaDec[i] - '0';
          } else if (hexaDec[i] >= 'U' && hexaDec[i] <= 'Z') {
            digit = hexaDec[i] - 'U' + 10;
          } else if (hexaDec[i] >= 'u' && hexaDec[i] <= 'z') {
            digit = hexaDec[i] - 'u' + 10;
          } else {
            fprintf(stderr, "Not hex\n");
            break;
          }
          value = (value * 16) + digit;
        }
    
        ...