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; 0. Let n = the number you are converting 1. Find p, the highest power of 16 which fits into ...

  1. #16
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    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

    Quote Originally Posted by brewbuck View Post
    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.
    ok I understand that you find p%16 which is the next digit or remainder as you where saying in step one would be just be represented as 16 in the case of the hightest power of 16.

    in ohter words r=n%16 or r=n/16 I guess I'm confused by the way you word it. cuz I know how to find the conversion from decimal to hex in the form of remainder for example
    921 in decimal
    what you mean by p=16 correct
    q=921%16=57//next digit
    r=921/16=57.5625 then .5625*16=9 remainder
    q=57%16=3//next digit
    r=57/16=3.5976 then .5976*16=9 remainder
    q=3%16=3 //next digit but in this case remainder
    solution is 399 in hex from 921 in dec
    Last edited by Sygo; 03-25-2008 at 01:31 PM.

  2. #17
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by Sygo View Post
    2. Divide n by p to get result q and remainder r.
    This line, in actual code, would look like this:

    Code:
    q = n / p;
    r = n % p;

  3. #18
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Quote Originally Posted by brewbuck View Post
    This line, in actual code, would look like this:

    Code:
    q = n / p;
    r = n % p;

    ok I know what you ment in code terms thats what I just wrote in the line ahead so p=16 the base or power correct

    so I'll explain it in code terms
    n=921;
    p=16;
    q=n/p;
    r=n%p;

    is that what you mean??????? if so we can stop doing the infinate loop now ha hahaha.

  4. #19
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    So understanding the concept of converting the remainders 10-15 in Hex A-F

Page 2 of 2 FirstFirst 12
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