Java实现二进制转换16进制(可以去掉空格)

import java.util.Scanner;

public class Test {

	public static String hexString2binaryString(String bString) {  
		
		bString = bString.replace("	", "");//去掉直接从word表格内复制出来的空格
		bString = bString.replace(" ", "");//去掉英文空格
		if (bString == null || bString.equals("") || bString.length() % 8 != 0)  
			return null;  
		StringBuffer tmp = new StringBuffer();  
		int iTmp = 0;  
		for (int i = 0; i < bString.length(); i += 4)  { 
			
			iTmp = 0;  
			for (int j = 0; j < 4; j++)  {  
				iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);  
			}  
			tmp.append(Integer.toHexString(iTmp));  
			
		}  
	    return tmp.toString();  
	}  
	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		Scanner scanner = new Scanner(System.in);
//		String [] hexString = new String [52];
//		for(int i = 0 ; i <52 ;i++){
//			String s = scanner.nextLine();
//			hexString[i] = hexString2binaryString(s);
//		}
//		for(int i = 0;i < 52 ;i++){
//			System.out.println(hexString[i]);
//		}
		System.out.println(hexString2binaryString("00000000000000 0000 000001"));
	}

}

  

posted @ 2016-06-19 21:07  墨染三秋丶  阅读(1773)  评论(0编辑  收藏  举报