Dec to Hex, Hex fraction to Dec fraction, and Dec fraction to Hex fraction conversion

This is a discussion on Dec to Hex, Hex fraction to Dec fraction, and Dec fraction to Hex fraction conversion within the C Programming forums, part of the General Programming Boards category; Hello to everyone I'm new to the c-programming board. I'll first start off with my first question. I know of ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    10

    Question 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. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    What's a hex fraction?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Hexiadecimal fraction I guess for example 1A23.A3

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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 %c%c\n", hex_chars[index1], hex_chars[index2]) ;
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    The next would be thought to convert 1A23.A3 to 6691.163 if thats would be the right calculation ha ha

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    ha ha. How are you planning on representing the dot?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    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. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    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 %x?
    Code:
    printf("The character is %02X\n",random);
    ???
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by vart View Post
    So many code just to avoid %x?
    Code:
    printf("The character is %02X\n",random);
    ???
    The OP asked....
    Quote 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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Quote Originally Posted by Todd Burch View Post
    The OP asked....

    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. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    also what do you mean by OP

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    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. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Sygo View Post
    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.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Quote Originally Posted by brewbuck View Post
    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. #15
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by Sygo View Post
    Ok thanks for clearing that up I'm still a newbie also instead of p/16 would it be better to do
    p%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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21