# Thread: hex to string problems

1. ## hex to string problems

I'v got this got to work for binary and deciaml by change it to a 2 or a 10 for the base, but when I use 16 for Hex, it doesn't produce the correct results for some reason. Any ideas?

Code:
```if(base =='h')
{
int temp = x, quotient, remainder;
int count = 0;
int i;
int p;
int q;

do{
quotient = temp/16;  //base
temp = quotient;
count++;
}
while(quotient != 0);

count++;
temp = x;
//char *aray = new char[count];

for(p=0, q=count-2; p<count-1; p++, q--)
{
quotient = temp / power(16, q); //base
remainder = temp % (int)power(16, q);  //base
string[p] =  quotient+48;
temp = remainder;
}
string[p] = 0;//giving null character to last element of aray.
return x;
}```

2. Originally Posted by ERIKh22a
I'v got this got to work for binary and deciaml by change it to a 2 or a 10 for the base, but when I use 16 for Hex, it doesn't produce the correct results for some reason. Any ideas?
Do you know why this works for bin and dec ascii digits?
Code:
`   string[p] =  quotient+48;`
ASCII '0' = decimal 48
ASCII '1' = decimal 49

etc.

but if you want ascii chars for 'a' - 'f' or 'A' - 'F', you have to do a little more work (not much)

Perhaps you can make a function that returns the ASCII char for all integers from 0 - 15. Call it, say, decimal_hex_value_to_ascii_char(), or something. Then do this:

Code:
`   string[p] =  decimal_hex_value_to_ascii_char(quotient);`
Regards,

Dave

3. Originally Posted by Dave Evans
Do you know why this works for bin and dec ascii digits?
Code:
`   string[p] =  quotient+48;`
ASCII '0' = decimal 48
ASCII '1' = decimal 49

etc.
Of course, that's non-portable.

Quzah.

4. Originally Posted by Dave Evans
Do you know why this works for bin and dec ascii digits?
Code:
`   string[p] =  quotient+48;`
ASCII '0' = decimal 48
ASCII '1' = decimal 49

etc.

but if you want ascii chars for 'a' - 'f' or 'A' - 'F', you have to do a little more work (not much)

Perhaps you can make a function that returns the ASCII char for all integers from 0 - 15. Call it, say, decimal_hex_value_to_ascii_char(), or something. Then do this:

Code:
`   string[p] =  decimal_hex_value_to_ascii_char(quotient);`
Regards,

Dave
Something liek this
Code:
```string[p] =  hexa(quotient);

int hexa( char hex )

{
switch(hex ) {
case 'F':
case 'f': return( 15);
case 'E':
case 'e': return( 14);
case 'D':
case 'd': return( 13);
case 'C':
case 'c': return( 12);
case 'B':
case 'b': return( 11);
case 'A':
case 'a': return( 10);
case '9': return( 9);
case '8': return( 8);
case '7': return( 7);
case '6': return( 6);
case '5': return( 5);
case '4': return( 4);
case '3': return( 3);
case '2': return( 2);
case '1': return( 1);
default: return( 0);
}```

Code:
```int hex_to_int(char hexnum)
{
char array[] = "0123456789abcdef";
char *ptr;

if((ptr = strchr(array, tolower(hexnum))))
return ptr - array;

return 0;
}```

6. Originally Posted by itsme86
Code:
```int hex_to_int(char hexnum)
{
char array[] = "0123456789abcdef";
char *ptr;

if((ptr = strchr(array, tolower(hexnum))))
return ptr - array;

return 0;
}```

Any other ideas? I couldn't get it to implement..

7. What problem did you have implementing it? Looks pretty simple to me.

As an aside though, I would have the function return -1 on error instead of 0. Otherwise there isn't any way to distinguish between an error or if "0" is passed.

8. I can only get it to print out numbers 10-15

Originally Posted by bithub
What problem did you have implementing it? Looks pretty simple to me.

As an aside though, I would have the function return -1 on error instead of 0. Otherwise there isn't any way to distinguish between an error or if "0" is passed.
Code:
```if(base =='h')
{
int temp = x, quotient, remainder;
int count = 0;
int i;
int p;
int q;

do
{
quotient = temp/16;
temp = quotient;
count++;
}while(quotient != 0);

count++;
temp = x;

for(p=0, q=count-2; p<count-1; p++, q--)
{
quotient = temp / power(16, q);
remainder = temp % (int)power(16, q);
string[p] =  hexa(quotient);
temp = remainder;
}

string[p] = 0;

return x;
}

int hexa(char hex)
{
if(hex <10)
return hex;
else

if (hex == 10)
return 'A';
if (hex == 11)
return 'B';
if (hex == 12)
return  'C';
if (hex == 13)
return 'D';
if (hex == 14)
return  'E';
if (hex == 15)
return 'F';

return 0;
}```

9. Nevermind got it to work