java hex to binary conversion code example
Example 1: java convert hex to binary method
private String parseHexBinary(String hex) {
String digits = "0123456789ABCDEF";
hex = hex.toUpperCase();
String binaryString = "";
for(int i = 0; i < hex.length(); i++) {
char c = hex.charAt(i);
int d = digits.indexOf(c);
if(d == 0) binaryString += "0000";
else binaryString += Integer.toBinaryString(d);
}
return binaryString;
}
Example 2: java convert hex to binary method
private static int[] parseHexBinary(String hex) {
String digits = "0123456789ABCDEF";
int[] binaryValue = new int[hex.length()*4];
long val = 0;
for(int i = 0; i < hex.length(); i++) {
char c = hex.charAt(i);
int d = digits.indexOf(c);
val = val*16 + d;
}
for(int i = 0; i < binaryValue.length; i++) {
binaryValue[i] = (int) (val%2);
val = val/2;
}
return binaryValue;
}