Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by Mukesh ( 4 years ago )
import java.util.Scanner;

public class BinaryConversion {
    // This function checks whether the input is binary or not
    boolean isBinary(long binary) {
        long temp = binary;
        while (temp > 0) {
            int digit = (int) temp % 10;
            if ((digit != 0) && (digit != 1)) {
                System.out.println("Invailed number: binary number should contain only 0 & 1");
                return false; // return false if any other digits other than 0 and 1 are found.
            }
            temp /= 10;
        }
        return true; // return true if no any other digits are found
    }

    // Function to conver Binary number into Decimal
    int binaryToDecimal(long binary) {
        int decimal = 0;
        int i = 0;
        while (binary > 0) {
            decimal += (binary % 10) * Math.pow(2, i);
            i++;
            binary /= 10;
        }
        return decimal;
    }

    int binaryToOctal(long binary) {
        int octal = 0;
        int decimal = binaryToDecimal(binary); // getting decimal value of the binary number
        int i = 1;
        while (decimal > 0) {
            octal += (decimal % 8) * i;
            i *= 10;
            decimal /= 8;
        }
        return octal;
    }

    String binaryToHexa(long binary) {
        String hexa = "";
        int decimal = binaryToDecimal(binary); // getting decimal value of the binary number
        // int decimal = 239;
        while (decimal > 0) {
            int rem = decimal % 16;
            if (rem >= 10) {
                switch (rem) {
                    case 10:
                        hexa = "A" + hexa;
                        // hexa = hexa + "A";
                        break;
                    case 11:
                    hexa = "B" + hexa;
                        // hexa = hexa + "B";
                        break;
                    case 12:
                    hexa = "C" + hexa;
                        // hexa = hexa + "C";
                        break;
                    case 13:
                    hexa = "D" + hexa;
                        // hexa = hexa + "D";
                        break;
                    case 14:
                    hexa = "E" + hexa;
                        // hexa = hexa + "E";
                        break;
                    case 15:
                    hexa = "F" + hexa;
                        // hexa = hexa + "F";
                        break;
                }
            } else {
                hexa = Integer.toString(rem) + hexa;
                // hexa = hexa + Integer.toString(rem); // concatenateing remainder by converting it into string
            }
            decimal /= 16;
        }
        // Reversing the string
        // String temp = "";
        // for (int i = 0; i < hexa.length(); i++) {
        //     temp = hexa.charAt(i) + temp; // charAt fuction get the character of string at given index
        // }
        // return temp;
        return hexa;
    }

    public static void main(String[] args) {
        // long binary = 11101111;
        Scanner sc = new Scanner(System.in);
        long binary;
        System.out.print("Enter binary input: ");
        binary = sc.nextLong();
        sc.close();

        BinaryConversion m = new BinaryConversion();

        if (m.isBinary(binary) == true) {
            // if the given input is binary then only the method below will execute.
            System.out.println("The decimal: " + m.binaryToDecimal(binary));
            System.out.println("The octal: " + m.binaryToOctal(binary));
            m.binaryToHexa(binary);
            System.out.println("The hexaDecimal: " + m.binaryToHexa(binary));
        }
    }
}

 

Revise this Paste

Your Name: Code Language: