168493 XML and Web Services (II/2546) Base 2 to Base 10 Let b be a digit in the number of base 2 Then for a number of base 2, B, that has n digits, we can write them as B = bn-1bn-2..b2b1b0 The total value of B in a decimal number, D, is D = (bn-1x2n-1) + (bn-2x 2n-2) + … + (b2x22) + (b1x21) + (b0x20) 178110: Computer Programming (II/2546) Dept. of Computer Engineering, Khon Kaen U.
178110: Computer Programming (II/2546) Examples: Base 2 Base 10 1 = 1x20 = 1 10 = 1x21 + 0x20 = 2 101 = 1x22 + 0x21 + 1x20 = 4 + 1 = 5 11001 = 1x24 + 1x23 + 0x22 + 0x21 + 1x20 = 16 + 8 + 1 = 25 111111 = 1x25 + 1x24 + 1x23 + 1x22 + 1x21 + 1x20 = 32 + 16 + 8 + 4 + 2 + 1 = 63 178110: Computer Programming (II/2546)
178110: Computer Programming (II/2546) Base 10 to Base 2 D = (bn-1x2n-1) + (bn-2x 2n-2) + … + (b2x22) + (b1x21) + (b0x20) Procedure: Keep dividing D with 2 until the number is less than 2 The answer is the concatenation of final quotient and all remainders 178110: Computer Programming (II/2546)
178110: Computer Programming (II/2546) Examples: Base 10 Base 2 13 = 11012 178110: Computer Programming (II/2546)