A Number Systems Calculator

I have assigned a single-day project for my Computer Science students to create a number systems calculator to wrap-up our discussions on numbering systems.

In this project, students have to create a converter that first asks which number system the original number is from (Decimal, Binary, Octal, or Hexadecimal). It will then ask for the number and will output the equivalent values in the other 3 number systems with each clearly labeled.

The students are starting with the following code from Github…

//Program Name: Number Converter
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Fall 2016

import java.util.*;
import java.lang.*;
import java.io.*;

public class converter{
    public static void main (String[] args) throws java.lang.Exception{
        Scanner menuChoice = new Scanner(System.in);
        System.out.println("Number Converter");
        System.out.println("");
        System.out.println("Which number system are you converting from?");
        System.out.println("1 - Decimal - Base 10");
        System.out.println("2 - Binary - Base 2");
        System.out.println("3 - Octal - Base 8");
        System.out.println("4 - Hexadecimal - Base 16");
        System.out.println("");
        System.out.print("Enter Your Selection_ ");
        int menu = menuChoice.nextInt();
        
        switch (menu) {
            case 1:
                {
                    Scanner myNumber = new Scanner(System.in);
                    System.out.print("Enter Your Decimal (Base 10) Number_ ");
                    int myDecimalNumber = myNumber.nextInt();
                    String myDecimalNumberBin = (Integer.toString(myDecimalNumber, 2));
                    String myDecimalNumberOct = (Integer.toString(myDecimalNumber, 8));
                    String myDecimalNumberHex = (Integer.toString(myDecimalNumber, 16));
                    System.out.println("The Decimal Number " + myDecimalNumber + " is equal to the following:");
                    System.out.println("");
                    System.out.println("Binary (Base 2) = " + myDecimalNumberBin);
                    System.out.println("Octal (Base 8) = " + myDecimalNumberOct);
                    System.out.println("Hexadecimal (Base 16) = " + myDecimalNumberHex);
                    break;
                }
            case 2:
                {
                    Scanner myNumber = new Scanner(System.in);
                    System.out.print("Enter Your Binary (Base 2) Number_ ");
                    String myBinaryNum = myNumber.nextLine();
                    int myBinaryNumber = (Integer.parseInt(myBinaryNum,2));
                    String myBinaryNumberDec = (Integer.toString(myBinaryNumber, 10));
                    String myBinaryNumberOct = (Integer.toString(myBinaryNumber, 8));
                    String myBinaryNumberHex = (Integer.toString(myBinaryNumber, 16));
                    System.out.println("The Binary Number " + myBinaryNumber + " is equal to the following:");
                    System.out.println("");
                    System.out.println("Decimal (Base 10) = " + myBinaryNumberDec);
                    System.out.println("Octal (Base 8) = " + myBinaryNumberOct);
                    System.out.println("Hexadecimal (Base 16) = " + myBinaryNumberHex);
                    break;
                }
            case 3:
                {
                    //Code for Menu Option 3
                    break;
                }
            case 4:
                {
                    //Code for Menu Option 4
                    break;
                }
            default:
                System.out.println("Invalid Selection");
                break;
        }
    }
}

With the given information, they should be able to easily code cases 3 and 4 of the problem.

Students are asked to test their solutions using the following test case:

Base 10 Base 2 Base 8 Base 16
42 101010 52 2A

As you can see, the test case all comes down to a matter of “Life, the Universe and Everything!”.

Here is a possible final solution:

//Program Name: Number Converter
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Fall 2016

import java.util.*;
import java.lang.*;
import java.io.*;

public class converter{
    public static void main (String[] args) throws java.lang.Exception{
        Scanner menuChoice = new Scanner(System.in);
        System.out.println("Number Converter");
        System.out.println("");
        System.out.println("Which number system are you converting from?");
        System.out.println("1 - Decimal - Base 10");
        System.out.println("2 - Binary - Base 2");
        System.out.println("3 - Octal - Base 8");
        System.out.println("4 - Hexadecimal - Base 16");
        System.out.println("");
        System.out.print("Enter Your Selection_ ");
        int menu = menuChoice.nextInt();
        
        switch (menu) {
            case 1:
                {
                    Scanner myNumber = new Scanner(System.in);
                    System.out.print("Enter Your Decimal (Base 10) Number_ ");
                    int myDecimalNumber = myNumber.nextInt();
                    String myDecimalNumberBin = (Integer.toString(myDecimalNumber, 2));
                    String myDecimalNumberOct = (Integer.toString(myDecimalNumber, 8));
                    String myDecimalNumberHex = (Integer.toString(myDecimalNumber, 16));
                    System.out.println("The Decimal Number " + myDecimalNumber + " is equal to the following:");
                    System.out.println("");
                    System.out.println("Binary (Base 2) = " + myDecimalNumberBin);
                    System.out.println("Octal (Base 8) = " + myDecimalNumberOct);
                    System.out.println("Hexadecimal (Base 16) = " + myDecimalNumberHex);
                    break;
                }
            case 2:
                {
                    Scanner myNumber = new Scanner(System.in);
                    System.out.print("Enter Your Binary (Base 2) Number_ ");
                    String myBinaryNum = myNumber.nextLine();
                    int myBinaryNumber = (Integer.parseInt(myBinaryNum,2));
                    String myBinaryNumberDec = (Integer.toString(myBinaryNumber, 10));
                    String myBinaryNumberOct = (Integer.toString(myBinaryNumber, 8));
                    String myBinaryNumberHex = (Integer.toString(myBinaryNumber, 16));
                    System.out.println("The Binary Number " + myBinaryNumber + " is equal to the following:");
                    System.out.println("");
                    System.out.println("Decimal (Base 10) = " + myBinaryNumberDec);
                    System.out.println("Octal (Base 8) = " + myBinaryNumberOct);
                    System.out.println("Hexadecimal (Base 16) = " + myBinaryNumberHex);
                    break;
                }
            case 3:
                {
                    Scanner myNumber = new Scanner(System.in);
                    System.out.print("Enter Your Octal (Base 8) Number_ ");
                    String myOctalNum = myNumber.nextLine();
                    int myOctalNumber = (Integer.parseInt(myOctalNum,8));
                    String myOctalNumberDec = (Integer.toString(myOctalNumber, 10));
                    String myOctalNumberBin = (Integer.toString(myOctalNumber, 2));
                    String myOctalNumberHex = (Integer.toString(myOctalNumber, 16));
                    System.out.println("The Octal Number " + myOctalNumber + " is equal to the following:");
                    System.out.println("");
                    System.out.println("Decimal (Base 10) = " + myOctalNumberDec);
                    System.out.println("Binary (Base 2) = " + myOctalNumberBin);
                    System.out.println("Hexadecimal (Base 16) = " + myOctalNumberHex);
                    break;
                }
            case 4:
                {
                    Scanner myNumber = new Scanner(System.in);
                    System.out.print("Enter Your Hexadecimal (Base 16) Number_ ");
                    String myHexNum = myNumber.nextLine();
                    int myHexNumber = (Integer.parseInt(myHexNum,16));
                    String myHexNumberDec = (Integer.toString(myHexNumber, 10));
                    String myHexNumberBin = (Integer.toString(myHexNumber, 2));
                    String myHexNumberOct = (Integer.toString(myHexNumber, 8));
                    System.out.println("The Hexadecimal Number " + myHexNumber + " is equal to the following:");
                    System.out.println("");
                    System.out.println("Decimal (Base 10) = " + myHexNumberDec);
                    System.out.println("Binary (Base 2) = " + myHexNumberBin);
                    System.out.println("Octal (Base 8) = " + myHexNumberOct);
                    break;
                }
            default:
                System.out.println("Invalid Selection");
                break;
        }
    }
}

 

Base 2, 8, 10, & 16 Numbering

We are starting the UIL Computer Science season by looking at 4 major numbering systems. We are looking at the Base-2 (binary), Base-8 (octal), Base-10 (decimal), and Base-16 (hexadecimal) numbering systems.

Base-10

Base-10 is also known as decimal or abbreviated to dec.

This system is comprised of 10 unique digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

The decimal numbering system is arguably the easiest numbering system since it is what we are the most familiar with.

In the decimal numbering system, numbers are read from left-to right, but the place-values run from right-to-left. Each place-value position is 10 times larger than the position to its right.

Let’s examine the decimal number 1,22210

In this case, the 2 that is in the tens place is 10 times larger than the 2 that is in the ones place.

The 2 that is in the hundreds place is 10 times larger than the 2 that is in the tens place and 100 times larger than the 2 in the ones place.

The decimal numbering system working in multiples and fractions of 10.

Base 2

Base-2 is also known as binary or abbreviated to bin.

This system is comprised of 2 unique digits: 0 and 1.

Binary numbers are read from left-to-right like decimal numbers and the place-values run from right-to-left.

Place Value Position 02 = Base-10 Value 12 = Base-10 Value
1 010 110
2 010 210
3 010 410
4 010 810
5 010 1610
6 010 3210
7 010 6410
8 010 12810

Let’s look at the following binary number: 1012

When reading from right-to-left, the first place value holds a 12 and has a Base-10 value of 110.

The second place value holds a 02 and has a Base-10 value of 010.

The third place value holds a 12 and had a Base-10 value of 410.

To calculate the Base-10 value, we add the values of each place value together (110 + 010 + 410). In this case, the binary number 1012 has a Base-10 value of 510.

Let’s take a look at another binary number: 100111012

  • Place Value 1 – 12 = Base-10 Value = 110
  • Place Value 2 – 02 = Base-10 Value = 010
  • Place Value 3 – 12 = Base-10 Value = 410
  • Place Value 4 – 12 = Base-10 Value = 810
  • Place Value 5 – 12 = Base-10 Value = 1610
  • Place Value 6 – 02 = Base-10 Value = 010
  • Place Value 7 – 02 = Base-10 Value = 010
  • Place Value 8 – 12 = Base-10 Value = 12810

Add all of the Base-10 values together (12810 + 1610 + 810 + 410 + 110) and we get 15710. So, in this case, the binary number 100111012 has a Base-10 value of 15710.

Base-8

Base-8 is also known as octal or abbreviated to oct.

This system is comprised of 8 unique digits: 0, 1, 2, 3, 4, 5, 6, and 7.

Octal numbers are read from left-to-right, like Base-10 numbers but we must read the place-value position from right-to-left, like Base-2 numbers.

Let’s examine the octal 25618.

(28 X 83) + (58 X 82) + (68 X 81) + (18 X 80)

102410 + 32010 + 4810 + 110 = 1,39310

So, the octal 25618 has a Base-10 value of 1,39310.

In the solution, note the first digit in each set of parenthesis corresponds to the digits in the octal (2, 5, 6, and 1). We multiplied each of those by 8 raised to the power of their position in the octal from right-to-left with the first position having a power of 0.

Base 16

Base-16 is also known as hexadecimal or abbreviated to hex.

This system is comprised of 16 unique digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.

Hexadecimal numbers can be written one of two ways and either way is considered acceptable.

Method 1: 2AF316

Method 2: 0x2AF3

To convert a hexadecimal to Base-10 is very similar to the process for converting an octal to Base-10.

Let’s examine the hex 2AF316

(216 X 163) + (A16 X 162) + (F16 X 161) + (316 X 160)

Before we can continue, we need to discuss the Base-10 equivalents of the hexadecimal digits.

  • 016 = 010
  • 116 = 110
  • 216 = 210
  • 316 = 310
  • 416 = 410
  • 516 = 510
  • 616 = 610
  • 716 = 710
  • 816 = 810
  • 916 = 910
  • A16 = 1010
  • B16 = 1110
  • C16 = 1210
  • D16 = 1310
  • E16 = 1410
  • F16 = 1510

Now, we can continue:

(216 X 163) + (A16 X 162) + (F16 X 161) + (316 X 160)

(210 X 163) + (1010 X 162) + (1510 X 161) + (310 X 160)

8,19210 + 2,56010 + 24010 + 310 = 10,99510

Where Are They Used?

Base-10 is used as our standard counting and arithmetic system of the world around us. While it works great for our natural purposes, it does not lend itself to adequate use for internal processes of computer applications.

Base-2 is used as the fundamental basis of computing and boolean logic. Since Base-2 only has 2 digits this can equate to yes/no, on/off, true/false and any other absolute polar response query.

Base-8 is used in computer systems that parse 12-bit, 24-bit, and 36-bit binary words. Many of these computer systems are no longer in use and have been replaced with 32-bit and 64-bit binary word systems. As such, octal-based computing is no longer considered an efficient way to process data.

Base-16 is used to represent numerical data in a more concise manner for programming purposes that isn’t as fundamental as Base-2.

An example is the HEX color system. Let’s look at the HEX color 9FBDDF16. HEX colors are broken into 3 parts (1st 2 character represent amount of red, 2nd 2 characters represent amount of green, and 3rd 2 characters represent the amount of blue).

9F16 is the amount of red in the color. BD16 is the amount of green in the color. DF16 is the amount of blue in the color.

  • In this example, we have 9F16 equal to 15910.
  • If we continue, we have BD16 equal to 18910.
  • Finally, we have DF16 equal to 22310.

It is much easier for a programmer to enter the following:

background-color: #9FBDDF;

as compared to:

background-color: rgb(159, 189, 223);