我正在編寫一個在十進制,二進制和十六進制之間轉換的程序。該程序編譯,但是當我去輸入二進制輸入到十六進制時,我得到一個異常「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