I have already completed my hex to integer conversion function which works properly with the provided driver. However, my integer to hex function gets stuck in an infinite loop and I have no clue why. The red section of my code is the function which is giving me problems. Everything else works; however, is a rough draft so please don't give me crap about my other functions or drivers. They are all under construction. The Second code is my instructors driver he provided my class with. The line in green is a line I changed to allocate the correct memory size.
Thank You!

blade71(106)% cat xbits.c
/*
 *  stubs for functions to study
 *  integer-hex conversions
 *
 */

#include "xbits.h" /* Just prototypes...same as file */

/* Helpful function to get around not being able to use strchr()
 * Converts hex to decimal value
 */
int hex_To_dec(int c)
{
        char hex_values[] = "aAbBcCdDeEfF";
        int i;
        int answer = 0;

        for (i=0; answer == 0 && hex_values[i] != '\0'; i++)
        {
                if (hex_values[i] == c)
                {
                answer = 10 + (i/2);
                }
        }

        return answer;
}

/* function represents the int n as a hexstring which it places in the
hexstring array */
void itox( char hexstring[], int n)
{
        printf("in itox, processing %d\n",n);
        int number;
        n = number;
        int i;
        hexstring[0] = '0';
        hexstring[1] = 'x';
        while (number != '0')
        {
        i = 2;
        hexstring[i] = (number%16) < 10 ? (number%16) : 'A' + (number%16) - 10;
        i++;
        }
}

/* function converts hexstring array to equivalent integer value  */
int xtoi(char hexstring[])
{
        printf("in xtoi, processing %s\n", hexstring);
        int answer = 0;
        int i = 0;
        int valid = 1;
        int hexit;

        if (hexstring[i] == '0')
        {
                ++i;
                if (hexstring[i] == 'x' || hexstring[i] == 'X')
                {
                        ++i;
                }
        }
        while(valid && hexstring[i] != '\0')
        {
                answer = answer * 16;
                if(hexstring[i] >='0' && hexstring[i] <= '9')
                {
                        answer = answer + (hexstring[i] - '0');
                }
                else
                {
                        hexit = hex_To_dec(hexstring[i]);
                        if (hexit == 0)
                        {
                                valid = 0;
                        }
                        else
                        {
                        answer = answer + hexit;
                        }
                }
                ++i;
        }
        if(!valid)
        {
                answer = 0;
        }

        return answer;
}

Driver Code (Instructor Provided):

#include <stdio.h>
#include "xbits.h"

#define ENOUGH_SPACE 1 /* not really enough space */

int main()
{
  char hexstring[2*sizeof(int) +1];
  int m=0, n = 0xABC45;
  itox( hexstring, n); 


  /* for stub testing: create a fake input string */
  strcpy( hexstring, "ABC45");
  m= xtoi(hexstring);

  printf("\t%12d %s %12d\n", n,hexstring, m);

  return 0;  /* everything is just fine */
}

However, my integer to hex function gets stuck in an infinite loop and I have no clue why.

You never change the value of number inside the loop. The algorithm should be dividing it by 16 and storing the result, right? Also, 0 and '0' are two completely different values. Be sure to double check whether you are working in a string context or a numeric context and use the right constants.

n = number;

This will probably give you problems as well. The expression should be the other way around because number is the variable you are trying to initialize.

Here is a new and improved version of itox that still does not work:

blade71(175)% gcc showxbits.c test.c -o showxbits
Undefined                       first referenced
 symbol                             in file
xtoi                                /var/tmp//ccYWJ1Yj.o
ld: fatal: Symbol referencing errors. No output written to showxbits
collect2: ld returned 1 exit status
blade71(176)% cat test.c
#include <stdio.h>
#include "xbits.h"
void itox( char hexstring[], int n)
{
        printf("in itox, processing %d\n",n);
        int number;
        number = n;
        int i = 2;
        hexstring[0] = '0';
        hexstring[1] = 'x';
        while (number != 0)
        {
                number = number % 16;
                if (number = 1)
                {
                hexstring[i] = '1';
                i++;
                }
                else if (number = 2)
                {
                hexstring[i] = '2';
                i++;
                }
                else if (number = 3)
                {
                hexstring[i] = '3';
                i++;
                }
                else if (number = 4)
                {
                hexstring[i] = '4';
                i++;
                }
                else if (number = 5)
                {
                hexstring[i] = '5';
                i++;
                }
                else if (number = 6)
                {
                hexstring[i] = '6';
                i++;
                }
                else if (number = 7)
                {
                hexstring[i] = '7';
                i++;
                }
                else if (number = 8)
                {
                hexstring[i] = '8';
                i++;
                }
                else if (number = 9)
                {
                hexstring[i] = '9';
                i++;
                }
                else if (number = 10)
                {
                hexstring[i] = 'A';
                i++;
                }
                else if (number = 11)
                {
                hexstring[i] = 'B';
                i++;
                }
                else if (number = 12)
                {
                hexstring[i] = 'C';
                i++;
                }
                else if (number = 13)
                {
                hexstring[i] = 'D';
                i++;
                }
                else if (number = 14)
                {
                hexstring[i] = 'E';
                i++;
                }
                else if (number = 15)
                {
                hexstring[i] = 'F';
                i++;
                }
                else if (number = 0)
                {
                hexstring[i] = '\0';
                i++;
                }
        }
}

In addition this problem could have been done using an enum instead of a long drawn-out if statement.

if(number = 0){}

what do u expect it do do?
That is an assignment statement. Read how to compare numbers.....

U are doing

number = number%16;

u are getting the least significant digit by that and u are loosing the remaining number.
U should have done

digit = number%16;
number = number/16;
/*compare digit now*/

here is how u can solve your problem in short

void itox(char hex[], int num)
{
     int digit;
     int i=0;
     whle(num>0)
     {
        digit = num%16;
        num = num/16;
        if(digit<10)
              hex[i++] = digit + '0';
        else
             hex[i++] = digit -10 + 'A';
     }
     if(i==0)/*if the number was less than 1*/
        hex[i++] = '0';
     hex[i] = '\0';
     /*reverse the string*/
    reverseStr(hex);
}
void reverseStr(char str[])
{
   int i, j;
   char c;
   for(i=0, j=strlen(str)-1; i<j; i++, j--)
   {
      c = str[i];
      str[i] = str[j];
      str[j] = c;
   }
}

Solved! :

blade71(42)% gcc showxbits.c xbits_2.c -o showxbits
blade71(43)% showxbits
in itox, processing 703557
in xtoi, processing ABC45
              703557 ABC45       703557
blade71(44)% cat xbits_2.c
/*
 *  stubs for functions to study
 *  integer-hex conversions
 *
 */

#include "xbits.h"

/* Helpful function to get around not being able to use strchr()
 * Converts hex to decimal value
 */
int hex_To_dec(int c)
{
        char hex_values[] = "aAbBcCdDeEfF";
        int i;
        int answer = 0;

        for (i=0; answer == 0 && hex_values[i] != '\0'; i++)
        {
                if (hex_values[i] == c)
                {
                answer = 10 + (i/2);
                }
        }

        return answer;
}

/* function represents the int n as a hexstring which it places in the
hexstring array */

void itox( char hexstring[], int n)
 {
        printf("in itox, processing %d\n",n);
        hexstring[0] = '0';
        hexstring[1] = 'x';
        int b,c=0, i=2;
        b=n;
        while (b>15)
        {
                hexstring[i]=b%16;
                b=b/16;
                i++;
                c++;
        }
        hexstring[i]=b;
        for (i=c;i>=0;--i)
        {
                if (hexstring[i]==10)
                hexstring[i] = 'A';
                else if (hexstring[i]==11)
                hexstring[i] = 'B';
                else if (hexstring[i]==12)
                hexstring[i] = 'C';
                else if (hexstring[i]==13)
                hexstring[i] = 'D';
                else if (hexstring[i]==14)
                hexstring[i] = 'E';
                else if (hexstring[i]==15)
                hexstring[i] = 'F';
                else
                hexstring[i]=hexstring[i];
        }
        return;
}

/* function converts hexstring array to equivalent integer value  */
int xtoi(char hexstring[])
{
        printf("in xtoi, processing %s\n", hexstring);
        int answer = 0;
        int i = 0;
        int valid = 1;
        int hexit;

        if (hexstring[i] == '0')
        {
                ++i;
                if (hexstring[i] == 'x' || hexstring[i] == 'X')
                {
                        ++i;
                }
        }
        while(valid && hexstring[i] != '\0')
        {
                answer = answer * 16;
                if(hexstring[i] >='0' && hexstring[i] <= '9')
                {
                        answer = answer + (hexstring[i] - '0');
                }
                else
                {
                        hexit = hex_To_dec(hexstring[i]);
                        if (hexit == 0)
                        {
                                valid = 0;
                        }
                        else
                        {
                        answer = answer + hexit;
                        }
                }
                ++i;
        }
        if(!valid)
        {
                answer = 0;
        }

        return answer;
}

Thank You Tomm for your help; also thank you dkalita for pointing out that I was improperly using a cascading if statement. I went ahead to chapter 3 and read the next section of the book which explains how to use ifs better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.