0

私は手動で 16 進コードをバイナリに変換する任務を負っています 整数をバイナリに変換しようとするたびにエラーが発生することを除いて、ほとんど機能しています たとえば、16 進数を 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 に答える