二进制转16进制JAVA代码

public class Binary2Hex
{
	public static void main(String[] args)
	{
		String bString ="10101000";//需要转的二进制代码
		System.out.println(binaryString2hexString(bString));
	}

	public static String binaryString2hexString(String bString)
	{
		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();
	}
}

posted @ 2014-08-08 22:16  meiyouor  阅读(264)  评论(0编辑  收藏  举报