Binary to Hex Conversion

This is a discussion on Binary to Hex Conversion within the C Programming forums, part of the General Programming Boards category; Dear friends, I am trying to write a function that will convert an integer to a hex string. I would ...

1. Binary to Hex Conversion

Dear friends, I am trying to write a function that will convert an integer to a hex string. I would like to use the switch method. The code below is my work in progress, the logic i'm trying to follow is:

take the decimal integer 'n' and divide by 16
the remainder 'm' will form the first hex digit - hexstring[]
I want to use '0' to represent '1', '1' to represent 'A" and on until remainder 15 (F).
Then i want to take the quotient and divide by 16 and the remainder will form the second hex digit, next to last.
i want to repeat this process until my string is filled up and if it stop with a quotient equal to 0, fill the rest of the hex string with '0'.

Code:
```int itox( char hexstring[], int n) {
int m, b, flag;
b = (m / 16);
m = m - (b * 16);
for (m = 0;m > 0; m++)
switch (m) {

case '1' :
m = 01;
break;

case '2' :
m = 02;
break;

case '3' :
m = 03;
break;

case '4' :
m = 04;
break;

case '5' :
m = 05;
break;

case '6' :
m = 06;
break;

case '7' :
m = 07;
break;

case '8' :
m = 08;
break;

case '9' :
m = 09;
break;

case '10' :
m = A;
break;

case '11' :
m = B;
break;

case '12' :
m = C;
break;

case '13' :
m = D;
break;

case '14' :
m = E;
break;

case '15' :
m = F:
break;

default:
flag = 0;
break;
}

return (hexstring[m++]);
}

}```
could someone help me with my syntax, when I pass on a decimal i have an invalid digit "8" in octal constant

2. Why is the subject of this thread "binary to hex conversion"? Your function is screwy enough that I can't really tell what's going on, but it sounds like you just want:
Code:
```char buffer[BUFSIZ];
sprintf(buffer, "&#37;X", n);```

3. re:

sorry for the screw up code,

can't use printf because it is not allowed.

Code:
```int itox( char hexstring[], int n) {    /* takes in an integer n */
int m, b, flag;
b = (m / 16);                               /* i divide by 16 */
m = m - (b * 16);                        /* then because b is an int i can find the remainder by subtracting this number from the original number*/
for (m = 0;m > 0; m++)              /* then i want to start a for loop saying if m is greater then zero, increment count by testing the cases below */
switch (m) {                               */
/* finally after checking the cases, put them into a string and return that. */```
if you have any better suggestiongs of how i should rewrite so is not so screwed up.

4. where do you assign something to hexstring?
reminder operator is &#37;
where do you use n?

5. Some things to consider:

You're using m uninitialized. It starts off as garbage because you never give it an initial value.
Code:
```b = (m / 16);
m = m - (b * 16);```
...can just be rewritten:
Code:
`m = m &#37; 16;`
b is unnecessary.

Next, your for loop will never run:
Code:
`for (m = 0;m > 0; m++)`
You initialize m to 0 and then the condition checks if m is greater than 0, which obviously it won't be. Your entire loop will just be skipped over.

This brings us to your switch statement. What if you simply did this instead:
Code:
```char hexdigits[] = "0123456789ABCDEF";
hexstring[index++] = hexdigits[m];```
As long as m is between 0 and 15 (inclusive), it will assign the correct hex digit to your hexstring.

Finally, don't forget to terminate hexstring with a '\0' after the loop to turn it into a string.

6. Whats m? Unless it could be anything when you are calculating what b is since its not initialized.

To loop through the string you could pass a pointer to the string in eg:
Code:
`int itox( char* hex_str)`
You will then need to loop through all the characters in the string something like:
Code:
```while(*hex_str)
{
*hex_str++;
}```
If you want to split the char into 2 nibbles, then use the modulus operator like vart said. Eg:
Code:
```left = this_char / 16;
right = this_char &#37; 16;```
Hope that helps.

7. re:

thank you for your suggestions! a lot

Code:
```char hexdigits[] = "0123456789ABCDEF";
hexstring[index++] = hexdigits[m];```
where does index++ come from?

thanks for the suggestion Mike about the pointer but we haven't covered that in class yet, so I am not familiar with that method yet.

but how will it know that if it gets a remainder 11 it suppose to be B? because of the order of where it is position in the hexdigits [] ?

Code:
```/* so far i got this i'll continue working on this to get what i want*/

int itox( char hexstring[], int m)

{
int m, flag;
m = m &#37; 16;
for (m = 0 ;; m++)
char hexdigits[] = "0123456789ABCDEF"
hexstring[index++] = hexdigits[m];
{
return (hexstring[m]);
}```

8. It starts at 0, each time through the loop it just increments by 1. You just build the string one character at a time that way.