Skip to content

Instantly share code, notes, and snippets.

@javamultiplex
Created September 20, 2017 11:53
Show Gist options
  • Save javamultiplex/00970024289715d4e495bdd6e5c65153 to your computer and use it in GitHub Desktop.
Save javamultiplex/00970024289715d4e495bdd6e5c65153 to your computer and use it in GitHub Desktop.
Binary to Decimal Converter
package com.javamultiplex.baseconversion;
import java.util.Scanner;
/**
*
* @author Rohit Agarwal
* @category Base Conversion
* @problem Convert Binary to Decimal
*
*/
public class BinaryToDecimal {
public static void main(String[] args) {
Scanner input = null;
try {
input = new Scanner(System.in);
System.out.println("Enter binary number : ");
String number = input.next();
if (isBinaryNumber(number)) {
int decimal = getDecimalNumber(number);
System.out.println("Decimal Number : " + decimal);
} else {
System.out.println("Please enter valid binary number.");
}
} finally {
if (input != null) {
input.close();
}
}
}
private static int getDecimalNumber(String number) {
// Converting String to StringBuilder.
StringBuilder string = new StringBuilder(number);
string = string.reverse();
int length = string.length();
int digit, power, sum = 0;
for (int i = 0; i < length; i++) {
digit = string.charAt(i) - 48;
power = (int) Math.pow(2, i);
sum = sum + digit * power;
}
return sum;
}
private static boolean isBinaryNumber(String number) {
/*
* Regular expression that matches string containing only binary
* digits[0-1].
*/
String pattern = "^[01]+$";
boolean result = false;
if (number.matches(pattern)) {
result = true;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment