Thread: Writing function which convert a string of hexadecimal to corresponding integer

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    11

    Writing function which convert a string of hexadecimal to corresponding integer

    I was trying to write a function int htoi(char )to convert a string of hexadecimal to its decimal integer value.

    Here's the code:
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <math.h>
    
    
    #define MAX 100
    
    
    int htoi(char hex[]);
    int main()
    {
    int result;
    char hex[MAX];
    result = htoi(hex);
    printf("%d", result);
    }
    
    
    int htoi(char hex[])
    {
    int num,i,j,k,h,szarray;      //szarray is the length of string of hexadecimal
    for(i=0,j=0; hex[i] != '\n' ; ++i)
    {
        scanf("%c",&hex[i]);
        j++;
    }
    szarray = j;
    int arr[szarray-2];    //We define arr array to store the values of the input hexadecimal numbers
    h=0;
    j=0;
    for (i=szarray-1 ; i>1; --i,++h,++j)
    {
    if (hex[i]>= 'A' && hex[i]<= 'F')
        {
            arr[j] = pow(16,h)*(hex[i]-55);
        }
    else if (hex[i]>= 'a' && hex[i]<= 'f')
        {
            arr[j] = pow(16,h)*(hex[i]-87);
        }
    else if (hex[i]>= '0' && hex[i]<= '9')
        {
            arr[j] = pow(16,h)*(hex[i]-48);
        }
    }
    num = 0;
    for (k=0; k < szarray - 2 ; ++k) //sums all the numbers contained in the array arr//
    {
        num = num + arr[k];
    }
    return num;
    }

    Well while compiling my IDE gave
    warning that: in htoi format %c expects argument type "char *" but argument 2 has type int
    Can anyone explain me what this means? The program crashes after inputting a string of hexadecimal so I guess that has something to do with what's wrong with my program.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I don't know what you're talking about. I enabled all possible warnings while compiling this, but my compiler( GCC 5.4 ) kept completely silent.

    Maybe you keep compiling a different version of the file?

    Or maybe... are those two includes on the same line in your code? If yes, put them in separate lines.
    Last edited by GReaper; 03-11-2017 at 10:02 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    11
    No those includes are in different lines. Btw did it work? Mine just keeps on taking input even after sending \n
    Last edited by Noobpati; 03-11-2017 at 10:12 AM.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    I am running on gcc 6.3.0 on Debian Linux.

    pow() takes two doubles, and returns a double.

    Is this a homework assignment? You do know that the Standard Library provides a function strtol() to perform the same function.

    Start with this, and let us know if still a problem.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Noobpati View Post
    Btw did it work? Mine just keeps on taking input even after sending \n
    Same. This happens because you check for equality before input! The only way this loop would stop is if it encountered a "random" newline in the array or after the program crashes due to buffer overflow.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    It's ridiculous to pass hex into the function when it's only used as a local variable. Presumably you are meant to read the input string in main and then pass that string into the function.

    Why do you have i and j in your input loop? They contain exactly the same value. You only need i. Actually, why are you reading the string this way at all? Why not read it all at once with %s?

    It is pointless to store a bunch of values in arr and then add them up. Why not get rid of arr and simply add up the values as you calculate them?

    You don't need to use pow. You can easily calculate the powers of 16 without it. Begin with the power value as 1 and start at the end of the string (with the rightmost character). As you move leftwards, multiply the power by 16.

    In your countdown loop, you are bizarrely stopping when i == 1. You need to go down to (and including) 0.

    Don't use numbers like 48, 55, and 87. Instead use '0', 'A'-10, and 'a'-10, respectively.

    EDIT:
    Now that I think about it, you don't need the power at all. Just read the chars from left to right (not backwards), determine the value of the char and then add it into num after multiplying num by 16.
    Code:
        int num = 0;
        int sz = strlen(hex);
    
        for (i = 0; i < sz; i++) {
            int value;
            
            // determine value of hex[i] ...
    
            num = num * 16 + value;
        }
    If you encounter a non-hex char, break out of the loop.
    Last edited by algorism; 03-11-2017 at 11:35 AM.

  7. #7
    Registered User
    Join Date
    Feb 2017
    Posts
    11
    Thanks!
    I just couldn't find a way to pass the string entered in the main to the function without also passing the string length but that would make the function a bit different than what was asked for.
    Last edited by Noobpati; 03-13-2017 at 07:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to convert string to integer
    By BIGDENIRO in forum C Programming
    Replies: 6
    Last Post: 11-21-2013, 09:04 PM
  2. to convert from string to integer
    By vopo in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2008, 06:32 AM
  3. convert string to hexadecimal
    By nocturna_gr in forum C Programming
    Replies: 3
    Last Post: 12-11-2007, 04:45 AM
  4. how do i convert a string to an integer??
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 12-11-2001, 09:21 PM
  5. FAQ how do i convert a string to an integer?? (C)
    By Unregistered in forum FAQ Board
    Replies: 1
    Last Post: 12-02-2001, 05:03 PM

Tags for this Thread