# G.3 Converting Decimals

#### Converting Decimals to Binary Numbers

In order to convert decimals to binaries, we reverse the process outlined in Section G.1 for converting a binary to a decimal.

 4110 = 20x2 + 1 Dividing 41 by 2, gives the quotient 20 and remainder 1. 2010 = 10x2 + 0 We again divide the current quotient 20 by 2. 1010 = 5x2 + 0 510 = 2x2 + 1 We repeat this procedure until ... 210 = 1x2 + 0 110 = 0x2 + 1 ... the quotient is 0. 4110 = 1010012

The divisor used in the steps above is the base of the target number system (binary, base 2). The binary value, 1010012, is represented by the remainders, with the last remainder as the left-most bit. Back substitution of the quotient gives the same result:

 4110 = (((((0x2 + 1)x2 + 0)x2 + 1)x2 + 0)x2 + 0)x2 + 1 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21+ 1x20 = 1010012

#### Converting Decimals to Octal and Hexadecimal Numbers

Analogously, we can apply the above procedure for converting an octal to a binary. The conversion for the decimal number 90 can be done as follows:

 9010 = 11x8 + 2 1110 = 1x8 + 3 110 = 0x8 + 1 9010 = 1328 = 0132

The remainder values represent the digits in the equivalent octal number: 1328. This can be verified by back substitution, which gives the following result:

 9010 = ((0x8 + 1)x8 + 3)x8 + 2 = 1x82 + 3x81 + 2x80 = 1328 = 0132

 9010 = 5x16 + 10 510 = 0x16 + 5 9010 = 5a16 = 0x5a

The remainders represent the digits of the number in the hexadecimal system: 5a. Back substitution gives the same result:

 9010 = (0x16+ 5)x16 +10 = 5x161 + ax160 = 5a16 = 0x5a  List of Figures  List of Tables  List of Examples  Foreword  Preface  Chapter 1. Basics of Java Programming  Chapter 2. Language Fundamentals  Chapter 3. Operators and Assignments  Chapter 4. Declarations and Access Control  Chapter 5. Control Flow, Exception Handling, and Assertions  Chapter 6. Object-oriented Programming  Chapter 7. Nested Classes And Interfaces  Chapter 8. Object Lifetime  Chapter 9. Threads  Chapter 10. Fundamental Classes  Chapter 11. Collections and Maps  Appendix A. Taking the SCPJ2 1.4 Exam  Appendix B. Objectives for the SCPJ2 1.4 Exam  Appendix C. Objectives for the Java 2 Platform Upgrade Exam  Appendix D. Annotated Answers to Review Questions  Appendix E. Solutions to Programming Exercises  Appendix F. Mock Exam  Appendix G. Number Systems and Number Representation  G.1 Number Systems  G.2 Relationship between Binary, Octal, and Hexadecimal Numbers  G.3 Converting Decimals  G.4 Representing Integers  Appendix H. About the CD  Single-User License Agreement