Back to index

Converting stuff

Experiment with the following forms. Try several numbers in each and then inspect the code which makes it all happen.
Decimal Value: Hexadecimal Value:

Decimal Value: Binary Value:

If you don't want to have to rely on a computer to do your conversions for you, then read the following sections carefully so you can learn how to do it for yourself.
Converting Decimal to Hexadecimal:
Decimal Value: 4333
Step One:   Determine largest power of 16 which will divide into 4233.
            Powers of Sixteen: 16, 256, 4096, 65536, etc.
Step Two:   Divide 4333 by 4096 and record quotient and remainder.  The
            quotient will be the left most digit of your hexadecimal
            number and the remainder will be used in the next step.
Step Three: Divide the remainder by the next power of sixteen.
            237/256=0 r 237
            The zero is added to the right of the first digit from
            step two and the remainder is used in the next step.
Step Four:  Divide the remainder by the next power of sixteen.
            237/16=14 r13
            The values 10...15 are represented by the letters A...F
            A fourteen is represented by the letter E which is added
            to the right of the number from step three.
Step Five:  There are no more powers of 16 left and so the remainder
            from the previous step is converted into a hex digit and 
            placed to the right of the previous digit.
Hexadecimal Value: 10ED

Converting Decimal to Binary:
Decimal Value: 4333
Step One:   Determine largest power of 2 which will divide into 4333.
            Powers of two: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
                           2048, 4096, 8192, etc.
Step Two:   Subtract 4096 from 4333: 4333-4096=237.  Place a one down
            as the left most digit.
Step Three: Place a zero to the right of the first digit for every power
            of two you skip.  You skip 2048, 1024, 512, and 256, so that's
            four zeros.
Step Four:  Subtract 128 from 237: 237-128=109.  Place a one down to the
            right of the last digit.
Step Five:  Subtract 64 from 109: 109-64=45. Place a one down to the right
            of the last digit.
Step Six:   Subtract 32 from 45:  45-32=13.  Place a one down to the right
            of the last digit.
Step Seven: Skip 16 and place a zero to the right of the last digit.
Step Eight: Subtract 8 from 13: 13-8=5. Place a one to the right of the last
            digit.
Step Nine:  Subtract 4 from 5. 5-4=1. Place a one to the right of the 
            last digit.
Step Ten:   Skip 2 and place a zero to the right of the last digit.
Step Eleven: Place a one as the right most digit.
Binary Value: 1 0000 1110 1101

Assignment: Create a conversion function to convert feet to meters and vice versa. Find the conversion factor on the Internet.