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;
}