0

大家好,我已经实现了一个将值从二进制转换为十六进制的解决方案。所以我想知道是否有比我的更优雅的解决方案(很肯定有)。我已经测试了该程序并且它正在运行。这是代码:

public class BinaryToHex
{
    public static void main(String[] args) 
    {
        String binary = "1110001101";
        binaryToHex(binary);
        //38d
    }

    public static void binaryToHex(String binaryValue)
    {
        StringBuilder sb = new StringBuilder(binaryValue);

        System.out.println("Original StringBuilder: " + sb);
        sb.reverse();

        System.out.println("reversed StringBuilder: " + sb);

        int convert = binaryValue.length();
        System.out.println("Legth of the binary: " +  convert);

        if(convert % 4 != 0)
        {
            while(convert % 4 != 0)
            {


                sb.append(0);
                convert ++;

                System.out.println("StringBuilder in loop: " + sb);
                System.out.println("Convert in loop: " + convert);
            }
        }
        sb.reverse();
        System.out.println("Ready StringBuilder for use? " + sb);
        String test = null;
//      String test = sb.toString();
        for(int i=0; i<sb.length(); i ++)
        {
            if(i % 4 == 0)
            {
                test = sb.substring(i, (i+4));
                System.out.print(getChar(test));
            }
        }
    }

    public static String getChar(String num)
    {
        String number = "";
        switch(num)
        {
            case "0000" : number = "0"; break;
            case "0001" : number = "1"; break;
            case "0010" : number = "2"; break;
            case "0011" : number = "3"; break;
            case "0100" : number = "4"; break;
            case "0101" : number = "5"; break;
            case "0110" : number = "6"; break;
            case "0111" : number = "7"; break;
            case "1000" : number = "8"; break;
            case "1001" : number = "9"; break;
            case "1010" : number = "A"; break;
            case "1011" : number = "B"; break;
            case "1100" : number = "C"; break;
            case "1101" : number = "D"; break;
            case "1110" : number = "E"; break;
            case "1111" : number = "F"; break;

        }
        return number;
    }

}
4

5 回答 5

2

这个怎么样:Integer.toHexString(Integer.valueOf(binary, 2))

于 2012-08-01T13:16:51.547 回答
1
Long.toHexString(Long.valueOf(binaryString, 2));

如果二进制字符串长于 63 位(long在 Java 中是有符号的):

new BigInteger(binaryString, 2).toString(16);
于 2012-08-01T13:16:58.253 回答
1

怎么用Integer.toString(Integer.parseInt(binaryValue,2),16);

于 2012-08-01T13:17:25.430 回答
1

显然,为简洁起见,我跳过了任何错误检查。

int numInDec = Integer.parseInt(binaryString, 2);
String hexString = Integer.toHexString(numInDec);

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html

编辑:我看到你想避开图书馆,为什么?这是您正在寻找的“更优雅”的解决方案。

于 2012-08-01T13:19:49.973 回答
1

在库实现中做我们认为理所当然的事情是一个有趣的练习。您可以通过使用计数器跟踪您的位置并防止反向操作来大大改进您的算法。开关块也相当不优雅。

这是我的尝试。没有声称它是高性能的,但我认为这是对您的建议的改进。

public class BinToHex {

    static char[] HEX = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    static int[] POW_2 = {1,2,4,8};
    public static String toHex(String bin) {
        int len = bin.length();
        char[] result = new char[(int)Math.ceil(len*1D/4)];
        int pos = result.length-1;
        int hexval = 0;
        for (int i=0; i<len;i++ ){
            hexval += (bin.charAt(len-i-1)-HEX[0]) * POW_2[i%4];
            if ((i+1)%4==0) {
                result[pos--] = HEX[hexval];
                hexval = 0;
            }
        }
        if (pos==0) {
            result[0] = HEX[hexval];
        }
        return new String(result);
    }

    public static void main(String [] param) {
        System.out.println(BinToHex.toHex("1")); // border case 1 char => 0x1
        System.out.println(BinToHex.toHex("101")); // testcase less than 4 chars => 0x5
        System.out.println(BinToHex.toHex("1011")); // testcase eq 4 chars => 0xB
        System.out.println(BinToHex.toHex("101101011011")); // testcase lenght % 4  = 0
        System.out.println(BinToHex.toHex("11101101011011")); // testcase lenght % 4  != 0
        System.out.println(BinToHex.toHex("000101101011011")); // testcase leading 0
    }

}
于 2012-08-01T15:30:58.513 回答