10 진수, 2 진수 및 16 진수로 변환하는 프로그램을 작성하고 있습니다. 프로그램이 컴파일되지만 16 진수로 바이너리를 입력하려고하면 "java.lang.StringIndexOutOfBoundsException : String index of range : 34"예외가 발생합니다. 이것이 첫 번째 문제입니다. 두 번째 문제는 16 진수에서 2 진수로 변환하면 다소 길어지고 (잘못된) 반환이된다는 것입니다. 두 가지 코드를 모두 포함했습니다. 올바른 방향으로 조금 움직여 주시면 대단히 감사하겠습니다.16 진수에서 16 진수로 이진수로 변환
진수로 진 :
/**
* 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)가 기수를 사용합니까? –
'A'사례로 시작하여 'else if'조건 바로 다음에 추가 세미콜론이 있어야합니다. – rgettman
이것은 과제를위한 것이며 자바의 내장 라이브러리 함수를 사용하여 변환 작업을 수행 할 수 없습니다. – user3727648