Program:
import java.util.*;
// Note: This is Just check of Number System covnert in Simple Methods Reversed and Upper, It converts strings of numbers from different number systems (bases) into decimal integers.
// Binary (base 2)
// Octal (base 8)
// Decimal (base 10)
// Hexadecimal (base 16)
// Any base from 2 to 36
class ConvertNumSys{
public static void main(String args[]){
System.out.println("Enter Binary Number(101):");
Scanner sc= new Scanner(System.in);
// Binary Covernations: Decimal, Octal, HexaDecimal
String binary = sc.nextLine();
if (binary.isEmpty() || !binary.matches("[01]+")) {
System.out.println("Invalid binary input. Please enter only 0s and 1s.");
return;
}
int decimal=Integer.parseInt(binary,2); // Binary to decimal (2) // Fist Decimal then, Convert Decimal Value to Octal and HexaDecimal
String octal=Integer.toOctalString(decimal); // Binary to Octal (8)
String hexadeci=Integer.toHexString(decimal).toUpperCase(); // Binary to HexaDecimal (16)
System.out.println("Done!!, Binary to Decimal:"+decimal);
System.out.println("Done!!, Binary to Octal :"+octal);
System.out.println("Done!!, Binary to HexaDecimal :"+hexadeci);
// Octal Covernations: Decimal, Binay, HexaDecimal
// int octal=Integer.parseInt("17", 8); // Octal to decimal (15)
// int hexadeci=Integer.parseInt("1F", 16); // Hexadecimal to decimal (31)
// Hexa Covernations: Decimal, Binay, Octal
// System.out.println("Octal to Decimal:"+ octal);
// System.out.println("HexaDecimal to Decimal:"+ hexadeci);
}
}
Answer:
Enter Binary Number(101):
1
Done!!, Binary to Decimal:1
Done!!, Binary to Octal :1
Done!!, Binary to HexaDecimal :1
0 Comments