Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3937340
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:03:27+00:00 2026-05-20T00:03:27+00:00

I am writing this code to convert a hex entry into its integer equivalent.

  • 0

I am writing this code to convert a hex entry into its integer equivalent. So A would be 10 and B would be 11 etc. This code acts weirdly, in that it seg. faults at random locations and including an extra newline character at times will get it to work. I am trying to debug it, just so I can understand what I am doing wrong here. Can anyone take a look and help me here ? Thanks a lot for your time.

/* Fixed working code for anyone interested */

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


            unsigned int hextoint(const char temp[])
            {

            int i;
            int answer = 0;
            int dec;
            char hexchar[] = "aAbBcCdDeEfF" ;


            for ( i=0; temp[i] != '\0'; i++ )
            {

                if ( temp[i] == '\0')
                {

                    return ;        
                }

                if (temp[i] == '0' || temp[i] == 'x' || temp[i] == 'X' )
                {       
                    printf("0");
                    answer = temp[i];
                }

                // compare each temp[i] with all contents in hexchar[]
                int j;
                int a = temp[i];
                for ( j=0; hexchar[j] != '\0'; j++)
                {
                    if ( temp[i] == hexchar[j] )
                    {
                    answer *= 16;
                    answer = answer + 10 + (j/2);
  //                    printf("%d\n",answer );
                    break;      
                    }
                }

            }

            return answer;  

            }


            main()
            {
            char *test[] = 
            {   "bad",
                "aabbdd"
                "0100",
                "0x1",
                "0XA",
                "0X0C0BE",
                "abcdef",
                "123456",
                "0x123456",
                "deadbeef", 
                "zog_c"
            };

            int answer=0;

            // Calculate the number of char's.
            int numberOfChars;
            numberOfChars = sizeof test /sizeof test[0];

            printf("main():Number of chars = %d\n",numberOfChars);
            int i;
            // Go through each character and convert Hex to Integers.
            for ( i = 0; i<numberOfChars;i++)
            {
                // Need to take the first char and then go through it and convert            
                                        it.
                answer = hextoint(test[i]);
                printf("%d\n",answer ); 
            }


            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:03:27+00:00Added an answer on May 20, 2026 at 12:03 am

    Let’s take a look.

    unsigned int hextoint(const char temp[])
    {
        int i;
        int answer = 0;
        char hexchar[] = "aAbBcCdDeEfF" ;
    
        for ( i=0; temp[i] != '\0'; i++ )
        {
            printf("In here");
            printf("%c\t",temp[i] );
        }
    
        return answer;  
    }
    

    This doesn’t seem to even try to do any conversion. It should always return 0, since answer is never assigned any other value. Normally, you’d do something like:

    for (i=0; input[i] != '\0'; i++) {
        answer *= 16;
        answer += digit_value(input[i]);
    }
    return answer;
    

    Where digit_value (obviously enough) returns the value of an individual digit. One way to do this is:

    int digit_value(char input) { 
        input = tolower(input);
        if (input >= '0' && input <= '9')
            return input - '0';
        if (input >= 'a' && input <= 'f')
            return input - 'a' + 10;
        return -1; // signal error.
    }
    

    Then, looking at main:

    main()
    {
    

    Depending on the “implicit int” rule is generally poor practice, at least IMO. It’s much better to specify the return type.

    // Calculate the number of char's.
    int numberOfChars;
    numberOfChars = sizeof test /sizeof test[0];
    

    This actually calculates the number of strings, not the number of chars.

    for ( i = 0; i<=numberOfChars;i++)
    

    Valid subscripts run from 0 through the number of items – 1, so this attempts to read past the end of the array (giving undefined behavior).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing code that looks similar to this: public IEnumerable<T> Unfold<T>(this T seed) {
I find myself writing code that looks like this a lot: set<int> affected_items; while
So I was writing some code today that basically looks like this: string returnString
I am writing this code. Here dt is input into the function, as well
I'm writing code like this, doing a little quick and dirty timing: var sw
I frequently find myself writing code like this: List<int> list = new List<int> {
Why does everyone tell me writing code like this is a bad practice? if
Yesterday, I found myself writing code like this: SomeStruct getSomeStruct() { SomeStruct input; cin
I have some code like this in a winforms app I was writing to
I got this doubt while writing some code. Is 'bool' a basic datatype defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.