0

Write a C function, that accepts a null-terminated string, containing a hexadecimal string, and returns the integer value. You cannot call any C library function, except strlen() to code the function. The decimal string will only contain 0-4 ASCII characters from ‘0’ through ‘9’ and ‘A’ through ‘F’. No error handling is required. If the string is empty, then return a value of 0.

I've constantly tried fixing my errors, but once I fix them, new errors pop up, resulting in me being confused.

#include <stdlib.h>  /*used for EXIT_SUCCESS */
#include <stdio.h>   /*used for printf */
#include <string.h>  /* used for strlen */
#include <stdbool.h> /* used for bool */
#include <math.h>


unsigned int hexStringTouint(const char str[], int length, int n[])
{
    int i, j;
    int intvalue = 0;
    int digit;
    for(i = (length-1), j = 0; i --, j++)
    {
        if(n[i]>='0' && n[i] <='9')
        {
            digit = n[i] - 0x30;
        }
        else if(n[i]>= 'A' && n[i] <= 'F')
        {
            switch(n[i])
            {
                case 'A': digit = 10; break;
                case 'B': digit = 11; break;
                case 'C': digit = 12; break;
                case 'D': digit = 13; break;
                case 'E': digit = 14; break;
                case 'F': digit = 15; break;
            }
        }
        intvalue += digit*pow(16,j);
    }
    printf("int value is %d\n", intvalue);
    return 0;
}

int main(void)
{
    int i, length, intvalue;
    unsigned char n[] = "";
    printf("Enter your hexadecimal string: ");
    scanf("%c\n", n);
    intvalue = 0;
    length = strlen(n);
    return EXIT_SUCCESS;
}

I am getting error messages saying expected ';' in 'for' statement specifier and how const char* converts between pointers and integers.

11
  • 2
    for needs 3 statements. you missed the condition to break the loop. it should be for(i = (length-1), j = 0; i >= 0; i --, j++) in your case. Commented Jun 22, 2019 at 5:41
  • 1
    Possible duplicate of C code to convert hex to int Commented Jun 22, 2019 at 5:47
  • 2
    don't use pow(16,j). Use 1 << j*4 instead Commented Jun 22, 2019 at 6:03
  • A hint: If char c is the variable with current hexdigit character: The values of digits '0' to '9' are c - '0'. Similar, the values of digits 'A' to 'F' are c - 'A' + 0xa. Usually, upper case letters are accepted as well as lower case letters. In this case, a toupper(c) wouldn't hurt. (It doesn't change any non-alpha character.) Altogether: if (isxdigit(c)) digit = isdigit(c) ? c - '0' : toupper(c) - 'A' + 0xa; - a one liner... ;-) Commented Jun 22, 2019 at 7:11
  • Related: stackoverflow.com/q/7084213/694576 Commented Jun 22, 2019 at 7:27

1 Answer 1

1

Much of OP's code is the right track, yet various coding errors exist.

Add test condition

// for(i = (length-1), j = 0; i --, j++)
//                         v 
for(i = (length-1), j = 0; i >= 0; i --, j++)

cannot call any C library function, except strlen()

// intvalue += digit*pow(16,j);
intvalue += digit*(1u << (4*j));

Return the value

Requirement "... and returns the integer value"

//int intvalue = 0;
//...
//printf("int value is %d\n", intvalue);
//return 0;

unsigned intvalue = 0;
...
printf("integer value is %u\n", intvalue);
return intvalue;

Buffer too small

// unsigned char n[] = "";
// scanf("%c\n", n);

char n[100] = "";
scanf("%99s", n);

hexStringTouint()

Function not called in main().

Other issues exist


A simpler approach

unsigned hexStringTouint2(const char *str) {
  unsigned value = 0;
  while (*str) {
    unsigned digit = hexCharTouint(*str);  // tbd code
    value = value*16 + digit;
    str++;
  }
  return value;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.