Please help me find the endless loop and how to fix it:

blade71(555)% showxbits_2
32
in itox, processing 32
Test Value (b div 16): 2
Value of b: 32
Value of b (# mod 16): 0

/*
 *  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_Val(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=0, TEST = 0;
        b=n;
        while (b>=16)
        {
                TEST = b/16;
                hexstring[i+2]=TEST;
                i++;
                printf("Test Value (b div 16): %d\n", TEST);
                printf("Value of b: %d\n", b);
                b = b%16;
                hexstring[i+2] = b;
                printf("Value of b (# mod 16): %d\n", b);
                i++;
        }
        while ( b>=0 && b<15 )
        {
                int useless = b;
                if (hexstring[i+2]==10)
                hexstring[i+2] = 'A';
                else if (hexstring[i+2]==11)
                hexstring[i+2] = 'B';
                else if (hexstring[i+2]==12)
                hexstring[i+2] = 'C';
                else if (hexstring[i+2]==13)
                hexstring[i+2] = 'D';
                else if (hexstring[i+2]==14)
                hexstring[i+2] = 'E';
                else if (hexstring[i+2]==15)
                hexstring[i+2] = 'F';
                else
                hexstring[i+2]=hexstring[i+2];
        useless--;
        b=b-useless;
        }
        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_Val(hexstring[i]);
                        if (hexit == 0)
                        {
                                valid = 0;
                        }
                        else
                        {
                        answer = answer + hexit;
                        }
                }
                ++i;
        }
        if(!valid)
        {
                answer = 0;
        }

        return answer;
}

I think first you need to simplify, so as that you can clearly follow and read it. For instance: that whole if(hexstring[i+2]==10 through 15) and then ending it with a redundantly unnecessary else hexstring[i+2]=hexstring[i+2]; , all of that could be summarized in a single line.

So crop off all the excess code and simplify; then, it should become apparent where what is happening to what.

I suppose you've made some mistake at while loop from line 39 to 50. Value of b can't fulfill the condition to end. This line made it
b = b%16;

Please help me find the endless loop and how to fix it:

/*
 *  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_Val(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=0, TEST = 0;
        b=n;
        while (b>=16)
        {
                TEST = b/16;
                hexstring[i+2]=TEST;
                i++;
                printf("Test Value (b div 16): %d\n", TEST);
                printf("Value of b: %d\n", b);
                b = b%16;
                hexstring[i+2] = b;
                printf("Value of b (# mod 16): %d\n", b);
                i++;
        }
        while ( b>=0 && b<15 )
        {
                int useless = b;
                if (hexstring[i+2]==10)
                hexstring[i+2] = 'A';
                else if (hexstring[i+2]==11)
                hexstring[i+2] = 'B';
                else if (hexstring[i+2]==12)
                hexstring[i+2] = 'C';
                else if (hexstring[i+2]==13)
                hexstring[i+2] = 'D';
                else if (hexstring[i+2]==14)
                hexstring[i+2] = 'E';
                else if (hexstring[i+2]==15)
                hexstring[i+2] = 'F';
                else
                hexstring[i+2]=hexstring[i+2];
        useless--;
        b=b-useless;
        }
        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_Val(hexstring[i]);
                        if (hexit == 0)
                        {
                                valid = 0;
                        }
                        else
                        {
                        answer = answer + hexit;
                        }
                }
                ++i;
        }
        if(!valid)
        {
                answer = 0;
        }

        return answer;
}
while ( b>=0 && b<15 )

this is where its getting infinite
the value of b is always >0 according to your code

i think it works fine with the following changes
if(b>0 && b <=15)
{
if ( code
replacing 10 with 'A'
:
:
15 with 'F' goes here)
else
copy as it is.
}
i dint find the use of variables "useless" and "b", so i removed.

Corrected:

void itox(char hexstring[], int n)
 {
        int b = 0, TEST = 0, index =0, r[100], i=0;
        printf("in itox, processing %d\n",n);
        hexstring[i++] = '0';
        hexstring[i++] = 'x';
        for (index = 0; n>=16; index++)
        {
        r[index] = n %16;
        n = n/16;
        printf("Value of n divided by 16: %d\n",n);
        }
        r[index] = n;
        for (;index>=0;index--)
        {
                if (r[index] == 0)
                {
                hexstring[i] = '0';
                i++;
                }
                else if (r[index] == 1)
                {
                hexstring[i] = '1';
                i++;
                }
                else if (r[index] == 2)
                {
                hexstring[i] = '2';
                i++;
                }
                else if (r[index] == 3)
                {
                hexstring[i] = '3';
                i++;
                }
                else if (r[index] == 4)
                {
                hexstring[i] = '4';
                i++;
                }
                else if (r[index] == 5)
                {
                hexstring[i] = '5';
                i++;
                }
                else if (r[index] == 6)
                {
                hexstring[i] = '6';
                i++;
                }
                else if (r[index] == 7)
                {
                hexstring[i] = '7';
                i++;
                }
                else if (r[index] == 8)
                {
                hexstring[i] = '8';
                i++;
                }
                else if (r[index] == 9)
                {
                hexstring[i] = '9';
                i++;
                }
                else if (r[index] == 10)
                {
                hexstring[i] = 'A';
                i++;
                }
                else if (r[index] == 11)
                {
                hexstring[i] = 'B';
                i++;
                }
                else if (r[index] == 12)
                {
                hexstring[i] = 'C';
                i++;
                }
                else if (r[index] == 13)
                {
                hexstring[i] = 'D';
                i++;
                }
                else if (r[index] == 14)
                {
                hexstring[i] = 'E';
                i++;
                }
                else if (r[index] == 15)
                {
                hexstring[i] = 'F';
                i++;
                }
                else
                {
                printf("Err %d\n", r[index]);
                }
        }
        return;
}

xtoi is now in an endless loop during the execution; because I never end hexstring with '\0'. That is the next thing I will be working on

Completed entirely:

blade71(60)% cat xbits.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_Val(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)
 {
        int b = 0, TEST = 0, index =0, r[100], i=0;
        printf("in itox, processing %d\n",n);
        hexstring[i++] = '0';
        hexstring[i++] = 'x';
        for (index = 0; n>=16; index++)
        {
        r[index] = n %16;
        n = n/16;
        }
        r[index] = n;
        r[index+1] = '\0';
        for (;index>=0;index--)
        {
                if (r[index] == 0)
                {
                hexstring[i] = '0';
                i++;
                }
                else if (r[index] == 1)
                {
                hexstring[i] = '1';
                i++;
                }
                else if (r[index] == 2)
                {
                hexstring[i] = '2';
                i++;
                }
                else if (r[index] == 3)
                {
                hexstring[i] = '3';
                i++;
                }
                else if (r[index] == 4)
                {
                hexstring[i] = '4';
                i++;
                }
                else if (r[index] == 5)
                {
                hexstring[i] = '5';
                i++;
                }
                else if (r[index] == 6)
                {
                hexstring[i] = '6';
                i++;
                }
                else if (r[index] == 7)
                {
                hexstring[i] = '7';
                i++;
                }
                else if (r[index] == 8)
                {
                hexstring[i] = '8';
                i++;
                }
                else if (r[index] == 9)
                {
                hexstring[i] = '9';
                i++;
                }
                else if (r[index] == 10)
                {
                hexstring[i] = 'A';
                i++;
                }
                else if (r[index] == 11)
                {
                hexstring[i] = 'B';
                i++;
                }
                else if (r[index] == 12)
                {
                hexstring[i] = 'C';
                i++;
                }
                else if (r[index] == 13)
                {
                hexstring[i] = 'D';
                i++;
                }
                else if (r[index] == 14)
                {
                hexstring[i] = 'E';
                i++;
                }
                else if (r[index] == 15)
                {
                hexstring[i] = 'F';
                i++;
                }
                else
                {
                printf("Err %d\n", r[index]);
                }
        }
        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_Val(hexstring[i]);
                        if (hexit == 0)
                        {
                                valid = 0;
                        }
                        else
                        {
                        answer = answer + hexit;
                        }
                }
                ++i;
        }
        if(!valid)
        {
                answer = 0;
        }

        return answer;
}

Desired Output:

blade71(108)% showxbits_2
4
in itox, processing 4
in xtoi, processing 0x4Ì
4 0x4Ì 0
128
in itox, processing 128
in xtoi, processing 0x80
128 0x80 128
32
in itox, processing 32
in xtoi, processing 0x20
32 0x20 32
64
in itox, processing 64
in xtoi, processing 0x40
64 0x40 64
2312321
in itox, processing 2312321
in xtoi, processing 0x234881
2312321 0x234881 2312321
123143536523452352643
in itox, processing -1850210173
Err -1850210173
in xtoi, processing 0x234881
-1850210173 0x234881 2312321
^[[12~^[[13~^[[14~^[[12~

Where I have marked red I was wondering why I get that funny 'I'. If someone could solve this it would be greatly appreciated! Thanks guys for all your help

Completed entirely:

blade71(60)% cat xbits.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_Val(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)
 {
        int b = 0, TEST = 0, index =0, r[100], i=0;
        printf("in itox, processing %d\n",n);
        hexstring[i++] = '0';
        hexstring[i++] = 'x';
        for (index = 0; n>=16; index++)
        {
        r[index] = n %16;
        n = n/16;
        }
        r[index] = n;
        r[index+1] = '\0';
        for (;index>=0;index--)
        {
                if (r[index] == 0)
                {
                hexstring[i] = '0';
                i++;
                }
                else if (r[index] == 1)
                {
                hexstring[i] = '1';
                i++;
                }
                else if (r[index] == 2)
                {
                hexstring[i] = '2';
                i++;
                }
                else if (r[index] == 3)
                {
                hexstring[i] = '3';
                i++;
                }
                else if (r[index] == 4)
                {
                hexstring[i] = '4';
                i++;
                }
                else if (r[index] == 5)
                {
                hexstring[i] = '5';
                i++;
                }
                else if (r[index] == 6)
                {
                hexstring[i] = '6';
                i++;
                }
                else if (r[index] == 7)
                {
                hexstring[i] = '7';
                i++;
                }
                else if (r[index] == 8)
                {
                hexstring[i] = '8';
                i++;
                }
                else if (r[index] == 9)
                {
                hexstring[i] = '9';
                i++;
                }
                else if (r[index] == 10)
                {
                hexstring[i] = 'A';
                i++;
                }
                else if (r[index] == 11)
                {
                hexstring[i] = 'B';
                i++;
                }
                else if (r[index] == 12)
                {
                hexstring[i] = 'C';
                i++;
                }
                else if (r[index] == 13)
                {
                hexstring[i] = 'D';
                i++;
                }
                else if (r[index] == 14)
                {
                hexstring[i] = 'E';
                i++;
                }
                else if (r[index] == 15)
                {
                hexstring[i] = 'F';
                i++;
                }
                else
                {
                printf("Err %d\n", r[index]);
                }
        }
        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_Val(hexstring[i]);
                        if (hexit == 0)
                        {
                                valid = 0;
                        }
                        else
                        {
                        answer = answer + hexit;
                        }
                }
                ++i;
        }
        if(!valid)
        {
                answer = 0;
        }

        return answer;
}

Desired Output:


Where I have marked red I was wondering why I get that funny 'I'. If someone could solve this it would be greatly appreciated! Thanks guys for all your help

better you supply the code how you are passing the the arguments to functions or the main program

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.