Reputation: 489
I found the following way hex to binary conversion:
String binAddr = Integer.toBinaryString(Integer.parseInt(hexAddr, 16));
While this approach works for small hex numbers, a hex number such as the following
A14AA1DBDB818F9759
Throws a NumberFormatException.
I therefore wrote the following method that seems to work:
private String hexToBin(String hex){
String bin = "";
String binFragment = "";
int iHex;
hex = hex.trim();
hex = hex.replaceFirst("0x", "");
for(int i = 0; i < hex.length(); i++){
iHex = Integer.parseInt(""+hex.charAt(i),16);
binFragment = Integer.toBinaryString(iHex);
while(binFragment.length() < 4){
binFragment = "0" + binFragment;
}
bin += binFragment;
}
return bin;
}
The above method basically takes each character in the Hex string and converts it to its binary equivalent pads it with zeros if necessary then joins it to the return value. Is this a proper way of performing a conversion? Or am I overlooking something that may cause my approach to fail?
Thanks in advance for any assistance.
Upvotes: 33
Views: 127515
Reputation: 141
Fast, and works for large strings:
private String hexToBin(String hex){
hex = hex.replaceAll("0", "0000");
hex = hex.replaceAll("1", "0001");
hex = hex.replaceAll("2", "0010");
hex = hex.replaceAll("3", "0011");
hex = hex.replaceAll("4", "0100");
hex = hex.replaceAll("5", "0101");
hex = hex.replaceAll("6", "0110");
hex = hex.replaceAll("7", "0111");
hex = hex.replaceAll("8", "1000");
hex = hex.replaceAll("9", "1001");
hex = hex.replaceAll("A", "1010");
hex = hex.replaceAll("B", "1011");
hex = hex.replaceAll("C", "1100");
hex = hex.replaceAll("D", "1101");
hex = hex.replaceAll("E", "1110");
hex = hex.replaceAll("F", "1111");
return hex;
}
Upvotes: 14
Reputation: 1
public static byte[] hexToBytes(String string) {
int length = string.length();
byte[] data = new byte[length / 2];
for (int i = 0; i < length; i += 2) {
data[i / 2] = (byte)((Character.digit(string.charAt(i), 16) << 4) + Character.digit(string.charAt(i + 1), 16));
}
return data;
}
Upvotes: -1
Reputation: 1
import java.util.*;
public class HexadeciamlToBinary
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the hexadecimal number");
String s=sc.nextLine();
String p="";
long n=0;
int c=0;
for(int i=s.length()-1;i>=0;i--)
{
if(s.charAt(i)=='A')
{
n=n+(long)(Math.pow(16,c)*10);
c++;
}
else if(s.charAt(i)=='B')
{
n=n+(long)(Math.pow(16,c)*11);
c++;
}
else if(s.charAt(i)=='C')
{
n=n+(long)(Math.pow(16,c)*12);
c++;
}
else if(s.charAt(i)=='D')
{
n=n+(long)(Math.pow(16,c)*13);
c++;
}
else if(s.charAt(i)=='E')
{
n=n+(long)(Math.pow(16,c)*14);
c++;
}
else if(s.charAt(i)=='F')
{
n=n+(long)(Math.pow(16,c)*15);
c++;
}
else
{
n=n+(long)Math.pow(16,c)*(long)s.charAt(i);
c++;
}
}
String s1="",k="";
if(n>1)
{
while(n>0)
{
if(n%2==0)
{
k=k+"0";
n=n/2;
}
else
{
k=k+"1";
n=n/2;
}
}
for(int i=0;i<k.length();i++)
{
s1=k.charAt(i)+s1;
}
System.out.println("The respective binary number is : "+s1);
}
else
{
System.out.println("The respective binary number is : "+n);
}
}
}
Upvotes: 0
Reputation: 847
public static byte[] hexToBin(String str)
{
int len = str.length();
byte[] out = new byte[len / 2];
int endIndx;
for (int i = 0; i < len; i = i + 2)
{
endIndx = i + 2;
if (endIndx > len)
endIndx = len - 1;
out[i / 2] = (byte) Integer.parseInt(str.substring(i, endIndx), 16);
}
return out;
}
Upvotes: 2
Reputation: 944
With all zeroes:
static String hexToBin(String s) {
String preBin = new BigInteger(s, 16).toString(2);
Integer length = preBin.length();
if (length < 8) {
for (int i = 0; i < 8 - length; i++) {
preBin = "0" + preBin;
}
}
return preBin;
}
Upvotes: 2
Reputation: 325
Integer.parseInt(hex,16);
System.out.print(Integer.toBinaryString(hex));
Parse hex(String) to integer with base 16 then convert it to Binary String using toBinaryString(int) method
example
int num = (Integer.parseInt("A2B", 16));
System.out.print(Integer.toBinaryString(num));
Will Print
101000101011
Max Hex vakue Handled by int is FFFFFFF
i.e. if FFFFFFF0 is passed ti will give error
Upvotes: 7
Reputation: 120516
BigInteger.toString(radix)
will do what you want. Just pass in a radix of 2.
static String hexToBin(String s) {
return new BigInteger(s, 16).toString(2);
}
Upvotes: 48