Skip to content

Instantly share code, notes, and snippets.

@ZacharyJacobCollins
Created October 17, 2016 20:25
Show Gist options
  • Save ZacharyJacobCollins/741159476a9e26da29a4952812029a3e to your computer and use it in GitHub Desktop.
Save ZacharyJacobCollins/741159476a9e26da29a4952812029a3e to your computer and use it in GitHub Desktop.
School project for binary to decimal conversions
import java.util.Scanner;
import java.lang.reflect.*;
public class Converter {
public String Type, Number, Excess, TwosComp, OnesComp, SignedMag;
public boolean isNegative = false;
//TODO add input.close not sure why throwing nosuch element exception
public static void main(String[] args) throws Exception {
boolean done = false;
while (done != true) {
Converter number = new Converter();
//Print the fields in the object
for (Field field : number.getClass().getFields()) {
String name = field.getName();
Object value = field.get(number);
System.out.printf(" %s : %s\n", name, value);
}
}
}
//Instantiates an object with the needed fields
public Converter() throws Exception {
this.Type = GetType();
this.Number = GetNumber(this.Type);
//If the number is decimal get the binary version of things
if (this.Type.equals("d")) {
this.Excess = GetBinaryExcess(this.Number);
this.OnesComp = GetBinaryOnesComp(this.Number);
this.TwosComp = GetBinaryTwosComp(this.Number);
this.SignedMag = GetBinarySignedMag(this.Number);
}
//If the number is binary get the decimal version of things
else if (this.Type.equals("b")){
this.Excess = GetDecimalExcess(this.Number);
this.OnesComp = GetDecimalOnesComp(this.Number);
this.TwosComp = GetDecimalTwosComp(this.Number);
this.SignedMag = GetDecimalSignedMag(this.Number);
}
}
//returns type of b or d - binary or decimal - and performs appropriate checks
public String GetType() {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the converter! \n\nWould you like to enter a decimal or 10-bit binary number? (Enter \'b\' or \'d\')");
String type = input.next();
while (!((type.equals("b") || type.equals("d")))) {
System.out.println("Enter \'b\' or \'d\'");
type = input.next();
}
return type;
}
//returns the number in either binary or decimal and performs appropriate checks
public String GetNumber(String type) {
Scanner input = new Scanner(System.in);
System.out.println("Awesome, now enter your number!");
String number = input.next();
//set the object field is negative to true
if (Integer.valueOf(number)<0) {
this.isNegative=true;
}
//check to make sure the binary number entered is greater than 10 digits
if (type.equals("b")) {
while (number.length() <=9 || number.length() >= 11 || number.matches(".*[2-9]")) {
System.out.println("Your did not meet the reqs. Please enter a 10-digit binary number.");
number = input.next();
}
}
//check to make sure that if a decimal number is entered it is in range
if (type.equals("d")) {
while (Integer.parseInt(number)>=512 || Integer.parseInt(number) <= -513) {
System.out.println("Number is outside of what may be represented with 10 bits! Enter a new number!.");
number = input.next();
}
}
return number;
}
//gets excess notation in binary format
public String GetBinaryExcess(String decimalString) {
int excess = Integer.parseInt(decimalString)+512;
return ConvertDecimalToBinary(Integer.toString(excess));
}
//gets excess notation in decimal format
public String GetDecimalExcess(String binaryString) {
int binaryInt = Integer.parseInt(binaryString, 2);
int fiveTwelve = Integer.parseInt("1000000000", 2);
int excess = binaryInt - fiveTwelve;
return String.valueOf(excess);
}
//receives decimal spits out ones comp in 10-bit binary form
public String GetBinaryOnesComp(String decimalString) throws Exception {
//if the number is negative, remove the sign, convert to binary, pad the binary, complement the binary
if (this.isNegative==true) {
decimalString = decimalString.replace("-", "");
String binaryString = this.ConvertDecimalToBinary(decimalString);
binaryString = ComplementBinary(decimalString);
return binaryString;
}
//if the number is positive convert it to 10-bit binary
else if (this.isNegative==false){
String binaryString = this.ConvertDecimalToBinary(decimalString);
return binaryString;
}
else {
throw new Exception("number was neither positive or neg. Check GetBinaryOnesComp()");
}
}
//receives binary spits out a string ones comp in decimal format
public String GetDecimalOnesComp(String binaryString) throws Exception {
if (binaryString.startsWith("1")) {
binaryString = ComplementBinary(binaryString);
String decimalString = ConvertBinaryToDecimal(binaryString);
return ("-" + decimalString);
}
else if (binaryString.startsWith("0")){
return ConvertBinaryToDecimal(binaryString);
}
else {
throw new Exception("number started with neither a 1 or 0. Check GetDecimalOnesComp()");
}
}
//receives a decimal string and converts to a binary 2s complimented string.
public String GetBinaryTwosComp(String decimalString) throws Exception {
//if the number is negative, subtract one, remove the sign, convert to binary, pad the binary, complement the binary,
if (this.isNegative == true) {
int decimalInt = Integer.parseInt(decimalString);
decimalInt-=1;
decimalString = String.valueOf(decimalInt);
decimalString = decimalString.replace("-", "");
String binaryString = this.ConvertDecimalToBinary(decimalString);
binaryString = ComplementBinary(binaryString);
return binaryString;
}
//if the number is positive convert it to 10-bit binary
else if (this.isNegative == false){
String binaryString = this.ConvertDecimalToBinary(decimalString);
return binaryString;
}
else {
throw new Exception("number was neither positive or neg. Check GetBinaryOnesComp()");
}
}
//gets the decimal version of twos comp given a binary string.
public String GetDecimalTwosComp(String binaryString) throws Exception {
if (binaryString.startsWith("1")) {
binaryString = this.ComplementBinary(binaryString);
String decimalString = ConvertBinaryToDecimal(binaryString);
int decimal = Integer.parseInt(decimalString);
decimal *= -1;
decimal -= 1;
return Integer.toString(decimal);
}
//if number is even return the binary string of the number.
else if (binaryString.startsWith("0")) {
return ConvertBinaryToDecimal(binaryString);
}
else {
throw new Exception("number started with neither a 1 or 0. Check GetDecimalOnesComp()");
}
}
//Get binary signed mag given a decimal number
public String GetBinarySignedMag(String decimalString) throws Exception {
if (this.isNegative == true) {
decimalString = decimalString.replace("-", "");
String binaryString = this.ConvertDecimalToBinary(decimalString);
return ("1" +binaryString);
}
else if (this.isNegative == false) {
String binaryString = this.ConvertDecimalToBinary(decimalString);
return ("0" + binaryString);
}
else {
throw new Exception("number was neither positive or neg. Check GetBinarySignedMag()");
}
}
//Get decimal signed mag given a binary number
public String GetDecimalSignedMag(String binaryString) throws Exception {
if (binaryString.startsWith("1")) {
binaryString = binaryString.substring(1, binaryString.length());
return ("-" + this.ConvertBinaryToDecimal(binaryString));
}
else if (binaryString.startsWith("0")){
binaryString = binaryString.substring(1, binaryString.length());
return this.ConvertBinaryToDecimal(binaryString);
}
else {
throw new Exception("number started with neither a 1 or 0. Check GetDecimalSignedMag()");
}
}
//helper method converts a decimal string to a binary string
public String ConvertDecimalToBinary(String decimalString) {
int decimalInt = Integer.parseInt(decimalString);
String binaryString =(Integer.toBinaryString(decimalInt));
binaryString = PadBinary(binaryString);
return binaryString;
}
//helper method converts a decimal string to a binary string
public String ConvertBinaryToDecimal(String binaryString) {
int decimalInteger = Integer.parseInt(binaryString, 2);
return Integer.toString(decimalInteger);
}
//helper method makes sure the binary is padded to ten digits.
public String PadBinary(String binaryString) {
int digitsOff = 10 - binaryString.length();
//If the binary number has less than 10 digits add 0 until up to ten.
if (digitsOff < 10){
for (int i=0; i<(digitsOff); i++) {
binaryString = "0" + binaryString;
}
}
return binaryString;
}
//helper method complements and pads a Binary Number. Accepts decimal string.
public String ComplementBinary(String binaryString) {
binaryString=PadBinary(binaryString);
binaryString=binaryString.replace("0", "9");
binaryString=binaryString.replace("1", "0");
binaryString=binaryString.replace("9", "1");
return binaryString;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment