Home > Enterprise >  Convert hex value to decimal in C but keep getting incorrect value, plus too few argument error when
Convert hex value to decimal in C but keep getting incorrect value, plus too few argument error when

Time:02-14

Hello I am trying to convert a hex value into the decimal form but for some reason the result I'm getting each time is negative and completely incorrect. Additionally I would like to take that decimal number and then convert it into it binary value. I have created func's for both but have run into the problem of "too few arguments" when calling my bin() func. If somebody could point me in the right direction and explain what I am doing wrong I would sincerely appreciate it.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define arraysize 20

int decimal() {

    int i = 0, val, len;
    char hex1[arraysize];
    long long dec = 0, base = 1;

    len = strlen(hex1);

    for (i = len--;i >= 0; i--) {
        if (hex1[1] >= '0' && hex1[i] <= '9') {
            dec  = (hex1[i] - 48) * base;
            base *= 16;
        }
        else if (hex1[i] >= 'A' && hex1[i] <= 'F') {
            dec  = (hex1[i] - 55) * base;
            base *= 16;
        }
        else if (hex1[i] >= 'a' && hex1[i] <= 'f') {
            dec  = (hex1[i] - 87) * base;
            base *= 16;
        }
    }
    printf("Your decimal value is: %lld\n",dec);
    return 0;
}

int bin(long long dec) {

    int a[10], i;

    for (i = 0; dec > 0; i  ) {
        a[i] = dec % 2;
        dec = dec / 2;
    }
    printf("\nThe binary value is: ");
    for (i = i - 1; i >= 0; i--) {
        printf("%d", a[i]);
    }
    return 0;
}

int main() {

    char hex1[arraysize];

    printf("Enter your HEX value: ");
    fflush(stdin);
    fgets(hex1, arraysize, stdin);

    decimal(hex1);
    bin();
}

CodePudding user response:

There are multiple problems in your code:

  • hex1 should be passed as an argument to decimal(). As posted, your code has undefined behavior because hex1 is an uninitialized local array.

  • i = len-- initializes i to the value of len, hence one position too far. Use i = len - 1 instead.

  • if (hex1[1] >= '0' && hex1[i] <= '9') uses hex[1] instead of hex[i]

  • you should use expressions with character constants '0', ('A' - 10) and ('a' - 10) instead of hard coded magical values 48, 55 and 87.

  • the array a is too short in function bin(). You should give it a length of at least 64.

  • the argument in bin() should have unsigned long long type.

  • fflush(stdin); has undefined behavior.

Here is a modified version:

#include <stdio.h>

unsigned long long decimal(const char *hex) {
    unsigned long long dec = 0;
    int i, c;

    for (i = 0; (c = hex[i]) != '\0'; i  ) {
        if (c >= '0' && c <= '9') {
            dec = dec * 16   (c - '0');
        } else
        if (c >= 'A' && c <= 'F') {
            dec = dec * 16   (c - 'A'   10);
        } else
        if (c >= 'a' && c <= 'f') {
            dec = dec * 16   (c - 'a'   10);
        }
    }
    return dec;
}

void bin(unsigned long long dec) {
    char a[65];
    int i;

    a[64] = '\0';
    for (i = 63;; i--) {
        a[i] = '0'   dec % 2;
        dec = dec / 2;
        if (dec == 0)
            break;
    }
    printf("The binary value is: %s\n", a);
}

int main() {
    char hex1[20];

    printf("Enter your HEX value: ");
    if (fgets(hex1, sizeof hex1, stdin)) {
        unsigned long long dec = decimal(hex1);
        printf("Your decimal value is: %llu\n", dec);
        bin(dec);
    }
    return 0;
}

CodePudding user response:

The "too few arguments" error means exactly what it says: you are calling the bin() function with no arguments. In your function definition, you defined bin() as taking a long long dec argument. When you call bin(), you must give it an argument, like

bin(7);

You have made the opposite mistake in the line above:

decimal(hex1);

You defined decimal() as taking no arguments, yet you called it with a char[] argument. In your decimal function you can remove the line where you declare an array of chars, and instead make that an argument of the function:

int decimal(char* hex1) {

    int i = 0, val, len;
    long long dec = 0, base = 1;
    
    . . .

You may want to further research the semantics of passing an array as an argument to a function.

CodePudding user response:

Try this and replace char hex1[arraysize]="FFFFFFFFFFFFFFFF"; as needed:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define arraysize 20
#define bin_arraysize 64

unsigned long long decimal(char* hex1) {

        long long dec = 0, base = 1;
        int len = strlen(hex1);
        for(int i = len-1;i >=0; i--) {
               if(hex1[i] >= '0' && hex1[i] <= '9') {
                        dec  = (hex1[i] - 48) * base;
                        base *= 16;
               }
                else if(hex1[i] >= 'A' && hex1[i] <= 'F') {
                        dec  = (hex1[i] - 55) * base;
                        base *= 16;
                }
               else if(hex1[i] >= 'a' && hex1[i] <= 'f') {
                       dec  = (hex1[i] - 87) * base;
                       base *= 16;
               }
        }
        printf("Your decimal value is: %llu\n",dec);
        return dec;
}


void bin(unsigned long long dec) {

        int a[bin_arraysize], i;

        for(i = 0; dec > 0; i  ) {
                a[i] = dec%2;
                dec = dec/2;
        }
        printf("\nThe binary value is: ");
        for(i = i - 1; i >= 0; i--) {
                printf("%d", a[i]);
                }
        return;
}

int main() {

        char hex1[arraysize]="FFFFFFFFFFFFFFFF";  
        unsigned long long dec=decimal(hex1);
        bin(dec);
}
  • Related