我正在编写一个在十进制,二进制和十六进制之间转换的程序。该程序编译,但是当我去输入二进制输入到十六进制时,我得到一个异常“java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:34”。这是第一个问题。第二个问题是,十六进制到二进制的转换给了我一些荒谬的冗长(和不正确)的回报。我已经包含了两者的代码。向正确的方向微调是非常感谢。二进制转换为十六进制和十六进制转换为二进制
二进制到十六进制:
/**
* Method that converts a binary number to its hexadecimal equivalent.
* @param no parameters
* @return returns void
*/
public void binToHex()
{
System.out.println("The binary number you enter will be converted to its hexidecimal equivalent.");
System.out.println("Please enter a binary number: ");
Scanner keyboard = new Scanner(System.in);
String bin = keyboard.nextLine();
String oldbin = bin;
bin = bin.replace(" ", "").trim();
StringBuffer hex = new StringBuffer("00000000000000000000000000000000");
//String hex1 = "";
int j = 0;
for (int i = 0; i < bin.length(); i++)
{
if (bin.substring(i, i+4).equals("0000"))
{
hex.setCharAt(j, '0');
}
else if (bin.substring(i, i+4).equals("0001"))
{
hex.setCharAt(j, '1');
}
else if (bin.substring(i, i+4).equals("0010"))
{
hex.setCharAt(j, '2');
}
else if (bin.substring(i, i+4).equals("0011"))
{
hex.setCharAt(j, '3');
}
else if (bin.substring(i, i+4).equals("0100"))
{
hex.setCharAt(j, '4');
}
else if (bin.substring(i, i+4).equals("0101"))
{
hex.setCharAt(j, '5');
}
else if (bin.substring(i, i+4).equals("0110"))
{
hex.setCharAt(j, '6');
}
else if (bin.substring(i, i+4).equals("0111"))
{
hex.setCharAt(j, '7');
}
else if (bin.substring(i, i+4).equals("1000"))
{
hex.setCharAt(j, '8');
}
else if (bin.substring(i, i+4).equals("1001"))
{
hex.setCharAt(j, '9');
}
else if (bin.substring(i, i+4).equals("1010"))
{
hex.setCharAt(j, 'A');
}
else if (bin.substring(i, i+4).equals("1011"))
{
hex.setCharAt(j, 'B');
}
else if (bin.substring(i, i+4).equals("1100"))
{
hex.setCharAt(j, 'C');
}
else if (bin.substring(i, i+4).equals("1101"))
{
hex.setCharAt(j, 'D');
}
else if (bin.substring(i, i+4).equals("1110"))
{
hex.setCharAt(j, 'E');
}
else if(bin.substring(i, i+4).equals("1111"))
{
hex.setCharAt(j, 'F');
}
i = i + 4;
j = j + 1;
}
System.out.println("The binary number you entered, " + oldbin + " is " + hex + " in hexadecimal.\n");
pw.print("The binary number you entered, " + oldbin + " is " + hex + " in hexadecimal.\n");
}
}
十六进制二进制:
/**
* Method that converts a hexadecimal number to its binary equivalent.
* @param no parameters
* @return returns void
*/
public void hexToBin()
{
System.out.println("The hexadecimal number you enter will be convered to its binary equivalent.");
System.out.println("Please enter a hexadecimal number: ");
Scanner keyboard = new Scanner(System.in);
String bin = keyboard.nextLine();
bin = bin.trim();
String binary = "";
for (int i = 0; i < bin.length(); i++)
{
if(bin.charAt(i) == '0')
{
binary = binary.concat("0000");
}
else if(bin.charAt(i) == '1')
{
binary = binary.concat("0001");
}
else if(bin.charAt(i) == '2')
{
binary = binary.concat("0010");
}
else if(bin.charAt(i) == '3')
{
binary = binary.concat("0011");
}
else if(bin.charAt(i) == '4')
{
binary = binary.concat("0100");
}
else if(bin.charAt(i) == '5')
{
binary = binary.concat("0101");
}
else if(bin.charAt(i) == '6')
{
binary = binary.concat("0110");
}
else if(bin.charAt(i) == '7')
{
binary = binary.concat("0111");
}
else if(bin.charAt(i) == '8')
{
binary = binary.concat("1000");
}
else if(bin.charAt(i) == '9')
{
binary = binary.concat("1001");
}
else if(bin.charAt(i) == 'A');
{
binary = binary.concat("1010");
}
if(bin.charAt(i) == 'B');
{
binary = binary.concat("1011");
}
if(bin.charAt(i) == 'C');
{
binary = binary.concat("1100");
}
if(bin.charAt(i) == 'D');
{
binary = binary.concat("1101");
}
if(bin.charAt(i) == 'E');
{
binary = binary.concat("1110");
}
if(bin.charAt(i) == 'F');
{
binary = binary.concat("1111");
}
}
System.out.println("The hexadecimal you entered, " + bin + " is " + binary + " in binary.\n");
pw.print("The hexadecimal you entered, " + bin + " is " + binary + " in binary.\n");
}
}
为什么不使用['toBinaryString()'](http://docs.oracle.com/javase/7 /docs/api/java/lang/Integer.html#toBinaryString%28int%29),['toHexString()'](http://docs.oracle.com/javase/7/docs/api/java/lang/ Integer.html#toHexString%28int%29)和['parseInt()'](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java。 lang.String,%20int%29)需要一个基数? – 2014-09-05 19:31:52
从'A'情况开始,您应该在应该删除的'else if'条件之后立即添加分号。 – rgettman 2014-09-05 19:36:42
这是一个任务,我不允许使用任何Java的内置库函数来完成转换。 – user3727648 2014-09-05 19:37:45