Kali ini saya akan membagikan tentang cara konfersi biner ke hexa dan sebaliknya, ..
langsung saj karena codingnya agak banyak, . .
import java.util.Scanner;
public class ConvertNumber {
public static void main(String[] args) {
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println(" JAVA PROGRAM TO CONVERT BIN TO HEX AND VICE VERSA");
System.out.println("---------------------------------------------------");
System.out.println();
System.out.print("Select an option: ");
int bin, bin1, bin2, hex, hex1, hex2;
int dec = 0, dec1 = 0, dec2 = 0;
int result = 0, result1 = 0, result2 = 0;
String output = "", output1 = "", output2 = "";
String hexa = "";
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);
int option = s.nextInt();
System.out.println();
switch (option) {
case 1: // Convert binary to hexadecimal
System.out.print("Input a binary number: ");
bin = s.nextInt();
String binString = Integer.toString(bin);
char[] b = binString.toCharArray();
boolean isBinary = true;
// Check whether it is a binary number or not
for(int i = 0; i < binString.length(); i++) {
if( (b[i] != '0') && (b[i] != '1') ) {
isBinary = false;
}
}
if (!isBinary) {
System.out.println("Invalid input!");
}
else if (isBinary && binString.length() > 8) {
System.out.println("Invalid input! Maximum 1 byte is allowed.");
} else {
// Devide the binary digits into two groups
int binGroup1 = bin % 10000;
int binGroup2 = bin / 10000;
if (binGroup1 >= 0 && binGroup2 == 0) {
// Convert binary to decimal
int i = 0;
while (i < 4) {
result = bin % 10;
dec = dec + (result * (int)Math.pow(2, i));
bin = bin / 10;
i++;
}
// Convert decimal to hexadecimal
if (dec < 10) {
hex = dec;
System.out.println("The hex number is: 0" + hex + "h");
}
else if (dec >= 10 && dec <= 15) {
switch (dec) {
case 10:
System.out.println("The hex number is: 0Ah");
break;
case 11:
System.out.println("The hex number is: 0Bh");
break;
case 12:
System.out.println("The hex number is: 0Ch");
break;
case 13:
System.out.println("The hex number is: 0Dh");
break;
case 14:
System.out.println("The hex number is: 0Eh");
break;
case 15:
System.out.println("The hex number is: 0Fh");
break;
default:
break;
}
}
}
else if (binGroup1 >= 0 && binGroup2 > 0) {
System.out.print("The hex number is: ");
// Convert binary to decimal on group 1
int j = 0;
while (j < 4) {
result1 = binGroup1 % 10;
dec1 = dec1 + (result1 * (int)Math.pow(2, j));
binGroup1 = binGroup1 / 10;
j++;
}
// Convert decimal to hexadecimal on group 1
if (dec1 < 10) {
hex1 = dec1;
output1 = "" + hex1;
}
else if (dec1 >= 10 && dec1 <= 15) {
switch (dec1) {
case 10:
output1 = "A"; break;
case 11:
output1 = "B"; break;
case 12:
output1 = "C"; break;
case 13:
output1 = "D"; break;
case 14:
output1 = "E"; break;
case 15:
output1 = "F"; break;
default:
break;
}
}
// Convert binary to decimal on group 2
int k = 0;
while (k < 4) {
result2 = binGroup2 % 10;
dec2 = dec2 + (result2 * (int)Math.pow(2, k));
binGroup2 = binGroup2 / 10;
k++;
}
// Convert decimal to hexadecimal on group 2
if (dec2 < 10) {
hex2 = dec2;
output2 = "" + hex2;
}
else if (dec2 >= 10 && dec2 <= 15) {
switch (dec2) {
case 10:
output2 = "A"; break;
case 11:
output2 = "B"; break;
case 12:
output2 = "C"; break;
case 13:
output2 = "D"; break;
case 14:
output2 = "E"; break;
case 15:
output2 = "F"; break;
default:
break;
}
}
System.out.println(output2 + output1 + "h");
}
}
break;
case 2: // Convert hexadecimal to binary
System.out.print("Input a hexadecimal number: ");
hexa = s.next();
if (hexa.length() == 1) {
if (hexa.charAt(0) >= 48 && hexa.charAt(0) <= 57 ||
hexa.charAt(0) >= 65 && hexa.charAt(0) <= 70) {
System.out.print("The binary number is: 0000");
if (hexa.charAt(0) >= 48 && hexa.charAt(0) <= 57){
dec = hexa.charAt(0) - 48;
}
else if (hexa.charAt(0) >= 65 && hexa.charAt(0) <= 70) {
dec = hexa.charAt(0) - 55;
}
for (int i = 0; i < 4; i++) {
result = dec / (int)(Math.pow(2, 3 - i));
bin = result % 2;
output = "" + bin;
System.out.print(output);
}
System.out.println("b");
} else {
System.out.println("Invalid input");
}
}
else if (hexa.length() == 2){
if (hexa.charAt(0) >= 48 && hexa.charAt(0) <= 57 &&
hexa.charAt(1) >= 48 && hexa.charAt(1) <= 57) {
System.out.print("The binary number is: ");
dec1 = hexa.charAt(0) - 48;
dec2 = hexa.charAt(1) - 48;
for (int i = 0; i < 4; i++) {
result1 = dec1 / (int)(Math.pow(2, 3 - i));
bin1 = result1 % 2;
output1 = "" + bin1;
System.out.print(output1);
}
for (int j = 0; j < 4; j++) {
result2 = dec2 / (int)(Math.pow(2, 3 - j));
bin2 = result2 % 2;
output2 = "" + bin2;
System.out.print(output2);
}
System.out.println("b");
}
else if (hexa.charAt(0) >= 48 && hexa.charAt(0) <= 57 &&
hexa.charAt(1) >= 65 && hexa.charAt(1) <= 70) {
System.out.print("The binary number is: ");
dec1 = hexa.charAt(0) - 48;
dec2 = hexa.charAt(1) - 55;
for (int i = 0; i < 4; i++) {
result1 = dec1 / (int)(Math.pow(2, 3 - i));
bin1 = result1 % 2;
output1 = "" + bin1;
System.out.print(output1);
}
for (int j = 0; j < 4; j++) {
result2 = dec2 / (int)(Math.pow(2, 3 - j));
bin2 = result2 % 2;
output2 = "" + bin2;
System.out.print(output2);
}
System.out.println("b");
}
else if (hexa.charAt(0) >= 65 && hexa.charAt(0) <= 70 &&
hexa.charAt(1) >= 48 && hexa.charAt(1) <= 57) {
System.out.print("The binary number is: ");
dec1 = hexa.charAt(0) - 55;
dec2 = hexa.charAt(1) - 48;
for (int i = 0; i < 4; i++) {
result1 = dec1 / (int)(Math.pow(2, 3 - i));
bin1 = result1 % 2;
output1 = "" + bin1;
System.out.print(output1);
}
for (int j = 0; j < 4; j++) {
result2 = dec2 / (int)(Math.pow(2, 3 - j));
bin2 = result2 % 2;
output2 = "" + bin2;
System.out.print(output2);
}
System.out.println("b");
}
else if (hexa.charAt(0) >= 65 && hexa.charAt(0) <= 70 &&
hexa.charAt(1) >= 65 && hexa.charAt(1) <= 70) {
System.out.print("The binary number is: ");
dec1 = hexa.charAt(0) - 55;
dec2 = hexa.charAt(1) - 55;
for (int i = 0; i < 4; i++) {
result1 = dec1 / (int)(Math.pow(2, 3 - i));
bin1 = result1 % 2;
output1 = "" + bin1;
System.out.print(output1);
}
for (int j = 0; j < 4; j++) {
result2 = dec2 / (int)(Math.pow(2, 3 - j));
bin2 = result2 % 2;
output2 = "" + bin2;
System.out.print(output2);
}
System.out.println("b");
} else {
System.out.println("Invalid input");
}
} else {
System.out.println("Invalid input! Maximum 1 byte is allowed.");
}
break;
default:
System.out.println("Invalid option");
break;
}
}
}
emoga bermanfaat, . .
0 Response to "Konversi Bilangan Biner ke Hexa"
Post a Comment