【发布时间】: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