I've just written the longest code in my short professional life:) It produces the expected output but I'm wondering if I could make it any simpler.
The second issue is that after verification the website says,
"Be sure that the toHex(String) method returns an empty string if it receives an empty string as input.
Be sure that the toBinary(String) method returns an empty string if it receives an empty string as input",
although these methods in my code start with the following lines (correspondingly):
(1)
if (binaryNumber.isEmpty())
return null;
(2)
if (hexNumber.isEmpty())
return null;
package en.codegym.task.pro.task09.task0908;
/*
Binary to hexadecimal converter
*/
public class Solution {
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
if (binaryNumber.isEmpty())
return null;
else {
for (int i = 0; i < binaryNumber.length(); i++) {
if (binaryNumber.charAt(i) != '0' && binaryNumber.charAt(i) != '1')
return null;
}
}
if (binaryNumber.length() % 4 != 0) { //We check if the length of the input string is a multiple of four
int extraZeroes = 4 - binaryNumber.length() % 4;
for (int i = 0; i < extraZeroes; i++) {
binaryNumber = "0" + binaryNumber; // we add extra zeroes at the beginning 0f the input string if necessary
}
}
//I'm not sure if I need to return the altered binaryNumber here
String hexNumber = "";
for (int i = 0; i < binaryNumber.length(); i += 4) { //we create a loop to go through the altered input string
String fourBits = binaryNumber.substring(i, (i + 4)); //we separate a substring of 4 bits
String fourBitsCoded = binaryToDecimal(fourBits); //string to string
fourBitsCoded = decimalToAF(fourBitsCoded); //string to string
hexNumber += fourBitsCoded;
//we concatenate the coded version of each four bits of the binary number to the hexNumber, which was empty in the beginning
}
return hexNumber;
}
public static String binaryToDecimal(String binary) {
if (binary.isEmpty()) return null;
else {
int decimalNumber = 0;
for (int i = 0; i < binary.length(); i++) {
int index = binary.length() - 1 - i;
int value = Character.getNumericValue(binary.charAt(index));
decimalNumber += (int) (value * Math.pow(2, i));
}
return String.valueOf(decimalNumber);
}
}
public static String decimalToAF(String stringDecimal) {
int tempNumber = Integer.valueOf(stringDecimal);
String result = "";//we need an integer here to compare it to 9
//if the decimal is less than or equal to 9, we return it as a string value
if (tempNumber <=9) result = stringDecimal;
//if the decimal is greater than 9 and less than 16, we convert it to a letter from a to f
else if (tempNumber > 9 && tempNumber < 16) {
String toSymbol;
switch (tempNumber) {
case 10:
toSymbol = "a";
break;
case 11:
toSymbol = "b";
break;
case 12:
toSymbol = "c";
break;
case 13:
toSymbol = "d";
break;
case 14:
toSymbol = "e";
break;
default:
toSymbol = "f";
}
result = toSymbol;
}
return result;
}
public static String toBinary(String hexNumber) {
//write your code here
if (hexNumber == "" || hexNumber == null)
return null; // if the input string is empty -> null
else {
String binaryNumber = "";
for (int i = 0; i < hexNumber.length(); i++) {
char tempChar = hexNumber.charAt(i);
if (Character.isDigit(tempChar)) {
int tempInt = Character.getNumericValue(tempChar);
String tempString = decimalToBinary(tempInt); //string representation of the binary number corresponding to tempInt
binaryNumber += tempString;
}
else if (Character.isLetter(tempChar) && Character.isLowerCase(tempChar)) {
if (tempChar >= 'f') return null;
else {
String tempString = letterToBinary(tempChar);//string rep of the binary number corresponding to the letter
binaryNumber += tempString;
}
}
else {
return null;
}
}
return binaryNumber;
}
}
public static String decimalToBinary(int decimalNumber) {
String binaryNumber = "";
if (decimalNumber == 0) {
binaryNumber = "0000";
}
while (decimalNumber > 0) {
binaryNumber = decimalNumber % 2 + binaryNumber;
decimalNumber = decimalNumber / 2;
}
return binaryNumber;
}
public static String letterToBinary(char letter) {
String result;
switch (letter) {
case 'a':
result = "1010";
break;
case 'b':
result = "1011";
break;
case 'c':
result = "1100";
break;
case 'd':
result = "1101";
break;
case 'e':
result = "1110";
break;
default:
result = "1111";
}
return result;
}
}