binary hex groups of four 0x1A4 = 0001 1010 0100 = binary octal ungrouped groups of three 000110100100 = 000 110 100 100 = 0644eight
Remember to group bits starting from the right. If the left group is one or two bits short, add zero bits on the left as needed.
Algorithm: Convert a number from base 10 to base B repn |
---|
place = 0; number = number to be converted while (number > 0 ) { digit[place] = number mod B ; number = number div B ; place = place + 1 ; } |
You know how to convert from Base B to Decimal. Now for converting from Decimal to base B. The algorithm converts number from base 10 representation to base B respresentation.
div means integer division and mod means modulo. number div B is the number of times B goes into number. number mod B is the left-over amount. For example 15 div 6 = 2 and 15 mod 6 = 3.
Here is an example: convert 5410 to hex representation. The base is 16. The first execution of the loop body calculates digit[0] the right-most digit of the hex number.