# Thread: Dec to Hex, Hex fraction to Dec fraction, and Dec fraction to Hex fraction conversion

1. ## Dec to Hex, Hex fraction to Dec fraction, and Dec fraction to Hex fraction conversion

Hello to everyone I'm new to the c-programming board.

I'll first start off with my first question.

I know of the itoa, strol, and sprintf way to convert a dec to Hex, but I'm actually trying to understand the logic behind actually writing the code. I have a serious migrain and can't put two and two together right now. Any ideas to break down the thought process in doing such conversions.

Thank You

2. What's a hex fraction?

3. Hexiadecimal fraction I guess for example 1A23.A3

4. So, to read a byte from memory, say 0xA3, and convert it to a character string is what you want to do I presume.

An easy way is to isolate each nibble of the byte (the "A" and the "3"), and then use that value in a lookup into a character array can be done like this:

Code:
```char hex_chars[] = "0123456789ABCDEF" ;
unsigned char random_char = 0xA3 ;
unsigned int index1, index2 ;
index1 = ( random_char & 0xF0 ) >> 4 ;
index2 = random_char & 0x0F ;
printf("The character is &#37;c%c\n", hex_chars[index1], hex_chars[index2]) ;```

5. The next would be thought to convert 1A23.A3 to 6691.163 if thats would be the right calculation ha ha

6. ha ha. How are you planning on representing the dot?

7. so far in my mind as an character bit have an control statement. have it loop till '.' is represtented in the array possibly and have it break into an inner control statement and increment the rest of the array inside that loop if that make sense at all ha ha ha ha. Hay my professor is making me the prototype for this program cuz I think he doesn't know how to do it himself. He's waiting for me to get done cuz he said he doesn't have the finished code for this one ha ha haha.

8. An easy way is to isolate each nibble of the byte (the "A" and the "3"), and then use that value in a lookup into a character array can be done like this:
So many code just to avoid &#37;x?
Code:
`printf("The character is %02X\n",random);`
???

9. Originally Posted by vart
So many code just to avoid %x?
Code:
`printf("The character is %02X\n",random);`
???
Originally Posted by OP
I know of the itoa, strol, and sprintf way to convert a dec to Hex, but I'm actually trying to understand the logic behind actually writing the code.
and I took that as they wanted to know how to do it without the %x.

10. Originally Posted by Todd Burch

and I took that as they wanted to know how to do it without the %x.

Yes he is correct I know of the easier ways of doing it I was wondering how to figure out the logic. Without using standard commands in the lib

11. also what do you mean by OP

12. Hexadecimal is based on powers of 16, and decimal is based on powers of 10. Forget about decimal for the moment. The number "10" doesn't come into any of these calculations, for reasons you should probably try to understand.

0. Let n = the number you are converting
1. Find p, the highest power of 16 which fits into n.
2. Divide n by p to get result q and remainder r.
3. Output q, which is a value between 0 and 15, as the next digit of the hex representation
4. Let n = r
5. Let p = p / 16
6. If p > 0, go to step 2

13. Originally Posted by Sygo
also what do you mean by OP
http://www.computerhope.com/jargon/o/op.htm
3. When talking about a user on a forum, op is short for original poster and is the first person started a topic on a forum.

14. Originally Posted by brewbuck
Hexadecimal is based on powers of 16, and decimal is based on powers of 10. Forget about decimal for the moment. The number "10" doesn't come into any of these calculations, for reasons you should probably try to understand.

0. Let n = the number you are converting
1. Find p, the highest power of 16 which fits into n.
2. Divide n by p to get result q and remainder r.
3. Output q, which is a value between 0 and 15, as the next digit of the hex representation
4. Let n = r
5. Let p = p / 16
6. If p > 0, go to step 2

Ok thanks for clearing that up I'm still a newbie also instead of p/16 would it be better to do
p%16;
So example p=15;
p=p%16 result= 15 now at this point the logic for the reading 'F' in Ascii

15. Originally Posted by Sygo
Ok thanks for clearing that up I'm still a newbie also instead of p/16 would it be better to do
p&#37;16;
You need to do both, actually. The quotient is used to produce the digit. The remainder is used as the next value to produce the next digit. I'm talking about step 2. The part where p is divided by 16 (step 5) is unrelated. It is just used to move down a power.

For the part about converting between a number in the range 0..15 and a hex digit, there are multiple ways, so try something first and if you have trouble we'll discuss.