1.二进制转16进制
public class Binary2Hex
{
public static void main(String[] args)
{
String bString = "1010101111001101";
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();
}
}
2.16进制转二进制
public class Hex2Binary
{
public static void main(String[] args)
{
String hexString = "ABCD";
System.out.println(hexString2binaryString(hexString));
}
public static String hexString2binaryString(String hexString)
{
if (hexString == null || hexString.length() % 2 != 0)
return null;
String bString = "", tmp;
for (int i = 0; i < hexString.length(); i++)
{
tmp = "0000"
+ Integer.toBinaryString(Integer.parseInt(hexString
.substring(i, i + 1), 16));
bString += tmp.substring(tmp.length() - 4);
}
return bString;
}
}