Skip to content

Instantly share code, notes, and snippets.

@essare
Created December 11, 2016 13:24
Show Gist options
  • Save essare/594f3a430ab6bab71e89b3c6d027652d to your computer and use it in GitHub Desktop.
Save essare/594f3a430ab6bab71e89b3c6d027652d to your computer and use it in GitHub Desktop.

Revisions

  1. essare created this gist Dec 11, 2016.
    317 changes: 317 additions & 0 deletions Conversion.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,317 @@
    /**
    * @Author: ESSARE AYOUB
    * Conversion Class
    * Convert from Bin to Dec - Oct - Hex
    */
    import java.util.*;
    public class Conversion {



    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int input = 0;
    System.out.println(" ||Chose an Operation:");
    System.out.println(" ||==> 1) Octal To Binary");
    System.out.println(" ||==> 2) Decimal To Binary");
    System.out.println(" ||==> 3) Hexa To Binary");
    System.out.println(" ||==> 4) From Binary To all");
    System.out.print(" Your Choice: ");
    Scanner scn = new Scanner(System.in);
    try {
    input = scn.nextInt();

    if ( input == 1){
    System.out.println("+============================+");
    System.out.println("| Octal To Binary Operation |");
    System.out.println("+============================+");
    // Analyzing Input
    OctToBinInput();
    scn.close();

    } else if ( input == 2 ){
    System.out.println("+============================+");
    System.out.println("| decimal To Binary Operation |");
    System.out.println("+============================+");
    // Analyzing Input
    DecToBinInput();
    scn.close();

    } else if ( input == 3 ) {
    System.out.println("+==========================+");
    System.out.println("| Hexa To Binary Operation |");
    System.out.println("+==========================+");
    // Analyzing Input
    HexToBinInput();
    scn.close();

    } else if ( input == 4 ) {
    System.out.println("+=====================+");
    System.out.println("| From Binary To All |");
    System.out.println("+=====================+");
    // Analyzing Input
    FromBinToAllInput();
    scn.close();

    } else {
    System.out.println("Invalid number! Please try Again");
    AnotherOperation();
    }
    } catch (InputMismatchException e) {
    System.out.println("Invalid number! Please try Again");
    AnotherOperation();
    }


    }
    public static void OctToBinInput(){
    Scanner input = new Scanner(System.in);
    System.out.print("Enter octal number: ");
    String oct = input.next();
    OctalToBin(oct);
    input.close();

    }

    public static void DecToBinInput(){
    Scanner input = new Scanner(System.in);
    System.out.print("Enter Decimal number: ");
    String dec = input.next();
    DecimalToBin(dec);
    input.close();

    }

    public static void HexToBinInput(){
    Scanner input = new Scanner(System.in);
    System.out.print("Enter Hexa number: ");
    String hex = input.next();
    HexaToBin(hex);
    input.close();

    }

    public static void FromBinToAllInput(){
    Scanner input = new Scanner(System.in);
    System.out.print("Enter Binary number: ");
    String bin = input.next();
    FromBinToAll(bin);
    input.close();

    }




    public static int OctalToBin(String octalNumber){
    String binary = "";
    for(int j = 0; j < octalNumber.length(); j++) {
    char num = octalNumber.charAt(j);
    num -= '0';
    if(num<0||num>7) {
    System.out.println("Invalid octal number! (<7 digits) Please try again.");
    OctToBinInput();
    return -1;
    }
    try {
    binary = Integer.toBinaryString(Integer.parseInt(octalNumber));
    } catch (NumberFormatException e){
    System.out.println("Number format is wrong! Please try again.");
    OctToBinInput();
    }

    }
    System.out.println(" Binary of " +octalNumber+ " is: " +binary);
    BinToOct(binary);
    BinToDec(binary);
    BinToHex(binary);
    return 1;
    }



    public static int DecimalToBin(String decimalNumber){
    String binary = "";
    for(int j = 0; j < decimalNumber.length(); j++) {
    char num = decimalNumber.charAt(j);
    num -= '0';
    if(num<0||num>9) {
    System.out.println("Invalid Decimal number! Please try again.");
    DecToBinInput();
    return -1;
    }
    try {
    binary = Integer.toBinaryString(Integer.parseInt(decimalNumber));
    } catch (NumberFormatException e){
    System.out.println("Number format is wrong! Please try again.");
    DecToBinInput();
    }

    }
    System.out.println(" Binary of " +decimalNumber+ " is: " +binary);
    BinToDec(binary);
    BinToOct(binary);
    BinToHex(binary);
    return 1;
    }


    public static int HexaToBin(String hexaNumber){
    String binary = "";
    for(int j = 0; j < hexaNumber.length(); j++) {
    // char num = hexaNumber.charAt(j);
    //num -= '0';
    if (isHexadecimal(hexaNumber)){
    int i = Integer.parseInt(hexaNumber, 16);
    binary = Integer.toBinaryString(i);
    }
    else {
    System.out.println("Invalid Hexa number! Please try again.");
    HexToBinInput();
    return -1;
    }


    }
    System.out.println(" Binary of " +hexaNumber+ " is: " +binary);
    BinToDec(binary);
    BinToOct(binary);
    BinToHex(binary);
    return 1;
    }

    public static boolean isHexadecimal(String text) {
    text = text.trim();

    char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F' };

    int hexDigitsCount = 0;

    for (char symbol : text.toCharArray()) {
    for (char hexDigit : hexDigits) {
    if (symbol == hexDigit) {
    hexDigitsCount++;
    break;
    }
    }
    }

    return true ? hexDigitsCount == text.length() : false;
    }


    public static long BinToDec(String bin){
    long decimalValue = 0;
    decimalValue = Long.parseLong(bin, 2);
    System.out.println("+=====================+");
    System.out.println("| Decimal: "+Long.toString(decimalValue));
    // System.out.println("+=====================+");
    return decimalValue;
    }

    public static long BinToOct(String bin){
    long octalValue = 0;
    octalValue = Long.parseLong(bin, 2);
    System.out.println("+=====================+");
    System.out.println("| Octal: "+Long.toOctalString(octalValue));
    // System.out.println("+=====================+");
    return octalValue;
    }

    public static long BinToHex(String bin){
    int hex = 0;
    int decimal = Integer.parseInt(bin,2);
    String hexStr = Integer.toString(decimal,16);
    System.out.println("+=====================+");
    System.out.println("| Hex: "+(hexStr));
    System.out.println("+=====================+");
    AskForAnotherOper();
    return hex;
    }

    public static void AnotherOperation(){
    int input = 0;
    System.out.println("\n ||Chose an Operation:");
    System.out.println(" ||==> 1) Octal To Binary");
    System.out.println(" ||==> 2) Decimal To Binary");
    System.out.println(" ||==> 3) Hexa To Binary");
    System.out.println(" ||==> 4) From Binary To all");
    System.out.print(" Your Choice: ");
    Scanner scn = new Scanner(System.in);
    try {
    input = scn.nextInt();

    if ( input == 1){
    System.out.println("+============================+");
    System.out.println("| Octal To Binary Operation |");
    System.out.println("+============================+");
    // Analyzing Input
    OctToBinInput();
    scn.close();

    } else if ( input == 2 ){
    System.out.println("+============================+");
    System.out.println("| decimal To Binary Operation |");
    System.out.println("+============================+");
    // Analyzing Input
    DecToBinInput();
    scn.close();

    } else if ( input == 3 ) {
    System.out.println("+==========================+");
    System.out.println("| Hexa To Binary Operation |");
    System.out.println("+==========================+");
    // Analyzing Input
    HexToBinInput();
    scn.close();

    } else if ( input == 4 ) {
    System.out.println("+=====================+");
    System.out.println("| From Binary To All |");
    System.out.println("+=====================+");
    // Analyzing Input
    FromBinToAllInput();
    scn.close();

    } else {
    System.out.println("Invalid number! Please try Again");
    AnotherOperation();
    }
    } catch (InputMismatchException e) {
    System.out.println("Invalid number! Please try Again");
    AnotherOperation();
    }




    }

    public static void AskForAnotherOper(){
    System.out.println("Calculate Another Operation [Y/n] ?");
    Scanner anotherInputScan = new Scanner(System.in);
    String anotherInput = anotherInputScan.next();
    if (anotherInput.equals("Y") || anotherInput.equals("y") || anotherInput.equals("Yes") || anotherInput.equals("YES") || anotherInput.equals("yes")){
    AnotherOperation();
    } else if (anotherInput.equals("n") || anotherInput.equals("N") || anotherInput.equals("no") || anotherInput.equals("NO")) {
    System.out.println("All Done! Thank you for using me :) ");
    } else {
    System.out.println("Invalid response! Please try again.");
    AskForAnotherOper();
    }
    anotherInputScan.close();
    }
    public static void FromBinToAll(String bin){
    try {
    BinToDec(bin);
    BinToOct(bin);
    BinToHex(bin);
    } catch (NumberFormatException e){
    System.out.println("Invalid Binary number! Please try again.");
    FromBinToAllInput();
    }

    }

    }