使用循环和切换在Java中将二进制转换为十六进制

时间:2015-04-02 15:00:32

标签: java

public static void main(String[] args) {

    Scanner ms = new Scanner(System.in);

    String binary = ms.nextLine();
    binary=binary.trim();

    //add leading zeroes if length divided by 4 has remainder.
    while (binary.length() % 4 != 0) binary = "0" + binary;

    String number = "";
    for (int i = 0; i < binary.length(); i += 4) {
       String num = binary.substring(i, i + 3);

       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;

        }
         System.out.println(number);
    }
}

我需要使用循环和开关操作来进行转换。做出这些改变之后。我得到二进制1111 1110的结果为F,然后是下一行的E.我该如何解决这个问题?我不想使用stringbuilder,因为我还没有学到这一点。还有其他简单的代码吗?

4 个答案:

答案 0 :(得分:0)

字符串不可变的

binary = binary.trim(); //not just binary.trim();

此外,您希望从索引0到3获取字符串,而不是0到4.所以它(i, i+3)

所以在这里它应该是:

for (int i = 0; i < binary.length(); i += 4) {
 String num = binary.substring(i, i + 3);

另外,取出底部的return语句,因为它在你进行一次迭代时会退出方法

答案 1 :(得分:0)

您的return语句位于for循环内,因此在第一次迭代后,您将从函数返回。此外,您在每次尝试时都会覆盖number。您应该使用number和用户StringBuilder替换append()

public static void main(String[] args) {

    Scanner ms = new Scanner(System.in);

    String binary = ms.nextLine();
    binary.trim();

    //add leading zeroes if length divided by 4 has remainder.
    while (binary.length() % 4 != 0) binary = "0" + binary;

    StringBuilder number = new StringBuilder();
    for (int i = 0; i < binary.length(); i += 4) {
       String num = binary.substring(i, i + 4);

       switch(num)
        {
            case "0000" : number.append("0"); break;
            case "0001" : number.append("1"); break;
            case "0010" : number.append("2"); break;
            case "0011" : number.append("3"); break;
            case "0100" : number.append("4"); break;
            case "0101" : number.append("5"); break;
            case "0110" : number.append("6"); break;
            case "0111" : number.append("7"); break;
            case "1000" : number.append("8"); break;
            case "1001" : number.append("9"); break;
            case "1010" : number.append("A"); break;
            case "1011" : number.append("B"); break;
            case "1100" : number.append("C"); break;
            case "1101" : number.append("D"); break;
            case "1110" : number.append("E"); break;
            case "1111" : number.append("F"); break;
        }             
        System.out.println(number.toString());
    }
    return;
}

此外,其他人也有心理,你的binary.trim()无法正常工作,需要binary = binary.trim()

答案 2 :(得分:0)

您的代码的当前问题是您在打印第一个return后立即number!删除return,它也会打印其他数字。

另外,正如Josh所指出的,你必须binary = binary.trim();,因为trim()就地改变字符串但返回字符串的剪裁版本。

最后,请注意,您可以用这个代替大部分代码......

int n = Integer.parseInt(binary, 2);
String s = Integer.toString(n, 16);
System.out.println(s.toUpperCase());

答案 3 :(得分:0)

因为你从循环的第一次迭代中return

无论如何,这里是你想要的代码片段,将二进制字符串转换为十六进制

static String binToHex(String binStr){
    while(binStr.length() % 4 != 0){
        binStr = "0" + binStr;
    }
    String hexString = "";
    binStr = new StringBuilder(binStr).reverse().toString();
    for(int index = 0, len = binStr.length(); index < len;){
        int num = 0;
        for(int indexInQuad = 0; indexInQuad < 4; indexInQuad++, index++){
            int bit=Integer.parseInt(String.valueOf(binStr.charAt(index)));
            num += (bit * Math.pow(2,indexInQuad));
        }
        hexString += Integer.toHexString(num).toUpperCase();
    }
    hexString = new StringBuilder(hexString).reverse().toString();
    return hexString;
}

它还为您保存了切换语句

只是传递二进制字符串值,它无缝地工作:D