0

我的任务是手动将十六进制代码转换为二进制我几乎可以让它工作,只是它在尝试将整数转换为二进制时出错例如将十六进制数字 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

4

2 回答 2

0

代替:

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);
}
于 2014-11-08T04:19:16.593 回答
0

这是因为您将数值而不是字符串附加到 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());
于 2014-11-08T04:29:21.977 回答