I have tested it with various online calculators, and it seems to output the correct solution. Additionally, I looked through the other questions pertaining to this task, but I must be missing something because I cannot figure out where I am going wrong. Any help, even if it is just an input that does not work correctly with my code, would be greatly appreciated.
Edit: I ended up solving this task by going at it differently than my code in this post. To solve it I just created if statements for each hexadecimal number/ symbol. Example:
if(hexNumber.charAt(i) == '0') binary += "0000";
I placed all of these in a for loop that went through each character in the given hexadecimal number.
I really do not see how this would work and my previous code would not as this code leaves a redundant 0000 at the beginning of the conversion if the hexadecimal begins with 0. Maybe this is why my previous code failed though. I cannot test it now as verification is complete. If anyone can confirm if this is the case that would be great.package en.codegym.task.pro.task09.task0908;
/*
Binary to hexadecimal converter
*/
import java.util.Objects;
public class Solution {
private static final String HEX = "0123456789abcdef";
public static void main(String[] args) {
String binaryNumber = "100111010000";
System.out.println("Binary number " + binaryNumber + " is equal to hexadecimal number " + toHex(binaryNumber));
String hexNumber = "9d0";
System.out.println("Hexadecimal number " + hexNumber + " is equal to binary number " + toBinary(hexNumber));
}
public static String toHex(String binaryNumber) {
//write your code here
String hexer = "";
String temp = "";
if((binaryNumber == null) || (Objects.equals(binaryNumber, ""))) {
return hexer;
} if (binaryNumber.length() % 4 != 0) {
while (binaryNumber.length() % 4 > 0) {
binaryNumber = "0" + binaryNumber;
}
} for(int checkValid = 0; checkValid < binaryNumber.length(); checkValid++) {
int check = binaryNumber.charAt(checkValid);
if(check > '1' || check < '0') {
return hexer;
}
}
for(int i = 0; i < binaryNumber.length() / 4; i++) {
for(int j = 0; j < binaryNumber.length(); j++) {
temp += binaryNumber.charAt(j);
if(j >= binaryNumber.length() - 1) {
int deci = 0;
for (int ij = 0; ij < temp.length(); ij++) {
deci += (temp.charAt(temp.length() - ij - 1) - '0') * Math.pow(2, ij);
if(ij >= temp.length() -1) {
while(deci > 0) {
hexer = HEX.charAt(deci % 16) + hexer;
deci = deci / 16;
}
return hexer;
}
}
}
}
}
return hexer;
}
public static String toBinary(String hexNumber) {
//write your code here
int deci = 0;
if (hexNumber == null || Objects.equals(hexNumber, "") ) {
return "";
}for(int check = 0; check < hexNumber.length(); check++) {
if((hexNumber.charAt(check) < '0') || (hexNumber.charAt(check) > '9' && hexNumber.charAt(check) < 'a') || hexNumber.charAt(check) > 'f') {
return "";
}
}
for (int i = 0; i < hexNumber.length(); i++) {
if (hexNumber.charAt(i) <= '9') {
deci = 16 * deci + HEX.charAt(hexNumber.charAt(i) - '0') - '0';
} else {
deci = 16 * deci + HEX.charAt(hexNumber.charAt(i) - 'a') - ('a' / 2) + 10;
}
} String binary = "";
while (deci > 0) {
binary = String.valueOf(deci % 2) + binary;
deci = deci / 2;
}
while(binary.length() % 4 !=0) {
binary = '0' + binary;
} while(binary.length() < 4) {
binary = '0' + binary;
}
return binary;
}
}