Answers for "convert hexadecimal to binary java"

0

java convert hex to binary method

/**
 * Method receives String hexadecimal value and returns a String of a binary representation
 * hexadecimal string format (ex.:"2FFA")
 * Only works with positive hexadecimal values (16xF does not work)
 * Uses 2 for loops (hex -> dec & dec -> bin)
 */
private static int[] parseHexBinary(String hex) {
		String digits = "0123456789ABCDEF";
		int[] binaryValue = new int[hex.length()*4];
		long val = 0;
		
		// convert hex to decimal
		for(int i = 0; i < hex.length(); i++) {
			char c = hex.charAt(i);
			int d = digits.indexOf(c);
			val = val*16 + d;
		}
		
		// convert decimal to binary
		for(int i = 0; i < binaryValue.length; i++) {
			
			binaryValue[i] = (int) (val%2);
			val = val/2;
		}
		
		return binaryValue;
	}
Posted by: Guest on December-22-2020

Code answers related to "convert hexadecimal to binary java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language