Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sep19;
- /*
- * Jacob Cacciamani
- * ID: E02073983
- * 9/19/19
- * Fall 2019
- * COSC 221
- * Section 1
- */
- import java.util.Scanner;
- /*
- * This program is a simple menu based program that can be used to
- * convert binary to decimal
- * and decimal to binary in the two's compliment system.
- */
- public class ConverterTwosCompliment {
- public static String twosCompliment(String bin) {
- char arr[] = bin.toCharArray();
- int len = bin.length();
- boolean addOne = true;
- // Flip Bits
- for (int i = len - 1; i >= 0; i--) {
- if (arr[i] == '0')
- arr[i] = '1';
- else
- arr[i] = '0';
- }
- // Add 1
- for (int i = len - 1; i >= 0 && addOne; i--) {
- if (arr[i] == '0') {
- arr[i] = '1';
- addOne = false;
- }
- else {
- arr[i] = '0';
- }
- }
- return new String(arr);
- }
- public static String convertToBinary(String dec) {
- int decimal = Integer.parseInt(dec);
- boolean negative = decimal < 0;
- String res = "";
- for (int i = 0; i < 7; i++) {
- res += (char)decimal % 2;
- decimal /= 2;
- }
- // If negative, use two's compliment, otherwise return
- return (negative) ? twosCompliment(reverseString(res + "0")) : reverseString(res + "0");
- }
- // Helper method for reversing binary strings so they aren't backwards
- public static String reverseString(String s) {
- String res = "";
- for (int i = s.length() - 1; i >= 0; i--) {
- res += s.charAt(i);
- }
- return res;
- }
- public static int convertToDecimal(String binary) {
- int sum = 0;
- int len = binary.length();
- boolean negative = false;
- if (binary.charAt(0) == '1') {
- binary = twosCompliment(binary);
- negative = true;
- }
- for (int i = 1; i < len; i++) {
- if (binary.charAt(i) == '1')
- sum += Math.pow(2, 7 - i);
- }
- return (negative) ? -sum : sum;
- }
- public static void main(String args[]) {
- int response;
- String num;
- Scanner scan = new Scanner(System.in);
- do {
- System.out.println("********************************************");
- System.out.println("Please enter 1 to convert decimal to binary");
- System.out.println("Please enter 2 to convert binary to decimal");
- System.out.println("Enter 3 to quit");
- response = scan.nextInt();
- if (response == 1) {
- System.out.println("Enter a decimal number between 127 and -128");
- num = scan.next();
- System.out.println("Your converted number is: " + convertToBinary(num));
- }
- else if (response == 2) {
- System.out.println("Enter an 8-bit binary string");
- num = scan.next();
- System.out.println("Your converted number is: " + convertToDecimal(num));
- }
- } while(response == 1 || response == 2);
- System.out.println("Goodbye!");
- scan.close();
- }
- }
- /*
- ********************************************
- Please enter 1 to convert decimal to binary
- Please enter 2 to convert binary to decimal
- Enter 3 to quit
- 1
- Enter a decimal number between 127 and -128
- -2
- Your converted number is: 11111110
- ********************************************
- Please enter 1 to convert decimal to binary
- Please enter 2 to convert binary to decimal
- Enter 3 to quit
- 2
- Enter an 8-bit binary string
- 01100100
- Your converted number is: 100
- ********************************************
- Please enter 1 to convert decimal to binary
- Please enter 2 to convert binary to decimal
- Enter 3 to quit
- 3
- Goodbye!
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement