08/11/2013

[Java] Hexadecimal to Binary

To convert a String representation of an hex to its binary String representation in Java:

 static String[]hex={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};  
 static String[]binary={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};  
   
 public static String hex2Binary(String hexStr){  
      String bin="";  
      for(int i=0;i<hexStr.length();i++){  
           String temp = String.valueOf(hexStr.charAt(i));  
           for(int j=0;j<hex.length;j++){  
                if(temp.equalsIgnoreCase(hex[j]))bin+=binary[j];  
           }  
      }  
      return bin;  
 }  

No comments:

Post a Comment

With great power comes great responsibility.

Da grandi poteri derivano grandi responsabilità.