import java.util.Scanner;
public class conversion {
public static boolean isBinary(String a) {
for (int i = 0; i < a.length(); i++) {
if ((a.charAt(i) != '0') && (a.charAt(i) != '1')) {
return false;
}
}
return true;
}
public static int binaryToDecimal(String a) {
int decimal = 0;
for (int i = a.length() - 1; i >= 0; i--) {
if (a.charAt(i) == '1') {
decimal += Math.pow(2, (a.length()) - i - 1);
}
}
return decimal;
}
public static String binaryToHex(String a) {
if (a.length() % 4 == 1) {
a = "000" + a;
}
if (a.length() % 4 == 2) {
a = "00" + a;
}
if (a.length() % 4 == 3) {
a = "0" + a;
}
String result = "";
String hex[] = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
String binary[] = new String[]{"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
for (int i = 0; i < a.length(); i += 4) {
for (int j = 0; j < binary.length; j++) {
if (binary[j].equals(a.substring(i, i + 4))) {
result += hex[j];
}
}
}
return result;
}
public static String binaryToOctal(String a) {
if (a.length() % 3 == 1) {
a = "00" + a;
}
if (a.length() % 3 == 2) {
a = "0" + a;
}
String result = "";
String octal[] = new String[]{"0", "1", "2", "3", "4", "5", "6", "7"};
String binary[] = new String[]{"000", "001", "010", "011", "100", "101", "110", "111"};
for (int i = 0; i < a.length(); i += 3) {
for (int j = 0; j < binary.length; j++) {
if (binary[j].equals(a.substring(i, i + 3))) {
result += octal[j];
}
}
}
return result;
}
public static boolean isHex(String a){
try {
Long.parseLong(a, 16);
return true;
}
catch (NumberFormatException ex) {
// Error handling code...
return false;
}}
public static String hexToBinary(String a){
String result="";
char hex[] = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8','9', 'A', 'B', 'C', 'D', 'E', 'F'};
String binary[] = new String[]{"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
for(int i=0;i<a.length();i++){
for(int j=0;j<hex.length;j++){
if(Character.toUpperCase(a.charAt(i))==(hex[j])){
result+=binary[j];
}}
}return result;}
public static int hexToDecimal(String a){
return binaryToDecimal(hexToBinary(a));
}
public static String hexToOctal(String a){
return binaryToOctal(hexToBinary(a));
}
public static boolean isOctal(int a){
if((a<0)||(a>7)){return false;}
return true;
}
public static String octalToBinary(int a){
int octal[] = new int[]{'0', '1', '2', '3', '4', '5', '6', '7'};
String binary[] = new String[]{"000", "001", "010", "011", "100", "101", "110", "111"};
String c=Integer.toString(a); String result="";
for(int i=0;i<c.length();i++){
for(int j=0;j<octal.length;j++){
if(c.charAt(i)==octal[j]){
result+=binary[j];
}}}
return result;
}
public static String octalToHex(int a){
return binaryToHex(octalToBinary(a));
}
public static int octalToDecimal(int a){
return binaryToDecimal(octalToBinary(a));
}
public static String decimalToBinary(double a){
String result="";
while(a>0){
if(a%2==1){result=1+result;}
else {result=0+result;}
a=a/2;}
return result;
}
public static String decimalToHex(double a){
return binaryToHex(decimalToBinary(a));
}
public static String decimalToOctal(double a){
return binaryToOctal(decimalToBinary(a));
}
this is what i did so far pls @Deque when you have some free time take a look at it and let me know if i need to fix anything or add anything to make it better !!