【问题标题】:Hex To binary - Using CharAt() with digits and appending numbers in a loop十六进制到二进制 - 在循环中使用带有数字和附加数字的 CharAt()
【发布时间】:2014-11-08 04:09:45
【问题描述】:

我的任务是手动将十六进制代码转换为二进制 我几乎可以让它工作,只是它在尝试将整数转换为二进制时出错 例如转动 HEX 数字 1,使其停止 所以如果我有 ABCDEFABC,一切都运行得很好 如果我有 ABCDEF123,它会停在 F 并由于某种原因给我一个 88

任何见解将不胜感激

这是我的代码:

    String Hex2="ABCDEF123";


    System.out.println("NEWLOOPTEST");

    StringBuilder hexstring = new StringBuilder();


    for (int x = 0; x <= 8; x++)
    {

    if (Hex2.charAt(x) == 'A')
    {
        hexstring.append(1010);

    }
    else if (Hex2.charAt(x) == 'B')
    {
        hexstring.append(1011);

    }
    else if (Hex2.charAt(x) == 'C')
    {
        hexstring.append(1100);
    }
    else if (Hex2.charAt(x) == 'D')
    {
        hexstring.append(1101);
    }
    else if (Hex2.charAt(x) == 'E')
    {
        hexstring.append(1110);
    }
    else if (Hex2.charAt(x) == 'F')
    {
        hexstring.append(1111);
    } 
    //works up to here
    else if (Hex2.charAt(x) == '0')
    {
        hexstring.append(0000);
    }
    else if (Hex2.charAt(x) == '1')
    {
        hexstring.append(0001);
    }
    else if (Hex2.charAt(x) == '2')
    {
        hexstring.append(0010);
    }
    else if (Hex2.charAt(x) == '3')
    {
        hexstring.append(0011);
    }
    else if (Hex2.charAt(x) == '4')
    {
        hexstring.append(0100);
    }
    else if (Hex2.charAt(x) == '5')
    {
        hexstring.append(0101);
    }
    else if (Hex2.charAt(x) == '6')
    {
        hexstring.append(0110);
    }
    else if (Hex2.charAt(x) == '7')
    {
        hexstring.append(0111);
    }
    else if (Hex2.charAt(x) == '8')
    {
        hexstring.append(1000);
    }
    else if (Hex2.charAt(x) == '9')
    {
        hexstring.append(1001);
    }
    else
    {
        System.out.println("error at char" + x );
    }
    }
    System.out.println("Hex To Decimal is " + hexstring.toString());

3994433

【问题讨论】:

    标签: loops binary append charat


    【解决方案1】:

    这是因为您将数值而不是字符串附加到 StringBuilder。引用你的二进制值。

    String Hex2="ABCDEF123";
    System.out.println("NEWLOOPTEST");
    StringBuilder hexstring = new StringBuilder();
    
    for (int x = 0; x <= 8; x++)
    {
        if (Hex2.charAt(x) == 'A')
        {
            hexstring.append("1010");
        }
        else if (Hex2.charAt(x) == 'B')
        {
            hexstring.append("1011");
        }
        else if (Hex2.charAt(x) == 'C')
        {
            hexstring.append("1100");
        }
        else if (Hex2.charAt(x) == 'D')
        {
            hexstring.append("1101");
        }
        else if (Hex2.charAt(x) == 'E')
        {
            hexstring.append("1110");
        }
        else if (Hex2.charAt(x) == 'F')
        {
            hexstring.append("1111");
        } 
        else if (Hex2.charAt(x) == '0')
        {
            hexstring.append("0000");
        }
        else if (Hex2.charAt(x) == '1')
        {
            hexstring.append("0001");
        }
        else if (Hex2.charAt(x) == '2')
        {
            hexstring.append("0010");
        }
        else if (Hex2.charAt(x) == '3')
        {
            hexstring.append("0011");
        }
        else if (Hex2.charAt(x) == '4')
        {
            hexstring.append("0100");
        }
        else if (Hex2.charAt(x) == '5')
        {
            hexstring.append("0101");
        }
        else if (Hex2.charAt(x) == '6')
        {
            hexstring.append("0110");
        }
        else if (Hex2.charAt(x) == '7')
        {
            hexstring.append("0111");
        }
        else if (Hex2.charAt(x) == '8')
        {
            hexstring.append("1000");
        }
        else if (Hex2.charAt(x) == '9')
        {
            hexstring.append("1001");
        }
        else
        {
            System.out.println("error at char" + x );
        }
    }
    
    System.out.println("Hex To Decimal is " + hexstring.toString());
    

    【讨论】:

    • 你是一个美丽的人。这是有道理的,它点击了。有趣的是,有时事情是如何运作的。非常感谢,让我省了很多麻烦
    • @vmina 确保修复其他帖子指出的那些二进制值。我刚刚更新了我的答案以反映这一点。
    【解决方案2】:

    替换:

    else if (Hex2.charAt(x) == '3')
    {
        hexstring.append(0010);
    }
    

    为此:

    else if (Hex2.charAt(x) == '3')
    {
        hexstring.append(0011);
    }
    

    并替换:

    else if (Hex2.charAt(x) == '7')
    {
        hexstring.append(1011);
    }
    

    为此:

    else if (Hex2.charAt(x) == '7')
    {
        hexstring.append(0111);
    }
    

    【讨论】:

    • 哦,那是我的疏忽。刚刚解决了这个问题,但它并没有解决我的问题。我的结果仍然是“101010111100110111101111189”。我应该得到 101010111100110111101111000100100011。谢谢你!
    猜你喜欢
    • 2013-11-21
    • 2015-01-21
    • 2013-11-24
    • 2018-09-08
    • 2020-01-10
    • 1970-01-01
    • 2017-09-13
    • 2018-05-17
    • 2015-08-08
    相关资源
    最近更新 更多