Any helpful tips would be much appreciated.
Edit: I found a logic error so I remedied it with the code below but it is still failing to validate.
if(hexNumber != null && !hexNumber.equals("")) {
for (int i = 0; i < hexNumber.length(); i++) {
placeHolder = hex.indexOf(hexNumber.substring(i, i + 1));
if(placeHolder != 0 && placeHolder != 1 && placeHolder != 2 && placeHolder != 3 && placeHolder != 4 && placeHolder != 5 && placeHolder != 6
&& placeHolder != 7) {
//Stores the remainder of the function, then adds it to the output variable in the reverse order that it is calculated
while (placeHolder != 0) {
ans = placeHolder % 2;
binary = ans + binary ;
placeHolder = placeHolder / 2;
}
binaryFinal = binaryFinal + binary;
binary = "";
}else {
if(placeHolder == 1){
binaryFinal = binaryFinal + "0001";
}else if(placeHolder == 2){
binaryFinal = binaryFinal + "0010";
}else if(placeHolder == 3){
binaryFinal = binaryFinal + "0011";
}else if(placeHolder == 4){
binaryFinal = binaryFinal + "0100";
}else if(placeHolder == 5){
binaryFinal = binaryFinal + "0101";
}else if(placeHolder == 6){
binaryFinal = binaryFinal + "0110";
}else if(placeHolder == 7){
binaryFinal = binaryFinal + "0111";
}else {
binaryFinal = binaryFinal + "0000";
}
}
}
while(binaryFinal.length() % 4 != 0) binaryFinal += "0";
return binaryFinal;
}
package en.codegym.task.pro.task09.task0908;
/*
Binary to hexadecimal converter
*/
public class Solution {
public static void main(String[] args) {
String binaryNumber = "100011001111";
System.out.println("Binary number " + binaryNumber + " is equal to hexadecimal number " + toHex(binaryNumber));
String hexNumber = "ff01";
System.out.println("Hexadecimal number " + hexNumber + " is equal to binary number " + toBinary(hexNumber));
}
public static String toHex(String binaryNumber) {
//write your code here
//Index variable, two placeholders, and an output variable
String hex = "0123456789abcdef";
String placeHolder;
double ans = 0;
String hexFinal = "";
if(binaryNumber != null && !binaryNumber.equals("")) {
while(binaryNumber.length() % 4 != 0) binaryNumber = "0" + binaryNumber;
//Grabs a section of the binary string in groups of four
for (int i = 0; i < (binaryNumber.length()); i += 4) {
placeHolder = binaryNumber.substring(i, 4 + i);
//Iterates through each # in the section of four multiplies it depending on its position and value
for (int j = 0; j <= placeHolder.length() - 1; j++) {
if (placeHolder.substring(j, j + 1).equals("1")) {
ans = Math.pow(2, (3 - j)) + ans;
}
}
hexFinal = hexFinal + hex.substring((int) ans, ((int) ans + 1));
ans = 0;
}
return hexFinal;
}
return "";
}
public static String toBinary(String hexNumber) {
//write your code here
//First three variables are placeholders and the last is the output
String hex = "0123456789abcdef";
int ans = 0;
String binary = "";
int placeHolder;
String binaryFinal = "";
if(hexNumber != null && !hexNumber.equals("")) {
for (int i = 0; i < hexNumber.length(); i++) {
placeHolder = hex.indexOf(hexNumber.substring(i, i + 1));
if(placeHolder != 0 && placeHolder != 1) {
//Stores the remainder of the function, then adds it to the output variable in the reverse order that it is calculated
while (placeHolder != 0) {
ans = placeHolder % 2;
binary = ans + binary ;
placeHolder = placeHolder / 2;
}
binaryFinal = binaryFinal + binary;
binary = "";
}else {
if(placeHolder == 1){
binaryFinal = binaryFinal + "0001";
}else {
binaryFinal = binaryFinal + "0000";
}
}
}
return binaryFinal;
}
return "";
}
}