import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main
{
public static String convertBinaryToHexadecimal(String binaryStr)
{
return new BigInteger(binaryStr, 2).toString(16);
}
public static String convertHexadecimalToBinary(String hexStr)
{
return new BigInteger(hexStr, 16).toString(2);
}
public static void main(String[] args)
{
String binaryStr = "01011111";
String myhexStr = convertBinaryToHexadecimal(binaryStr);
System.out.println(myhexStr);
String myBinStr = convertHexadecimalToBinary(myhexStr);
System.out.println(myBinStr);
}
}
Hi everyone. I'm trying to convert Binary to Hexadecimal and Vice Versa. I got a problem when I convert from Hexadecimal to Binary. My string Binary is "01011111", when I convert to Hexa the output is 5f which is correct but when I convert back to Binary, the output is 1011111 which is missing 0 at the front. How can I fix it?