2진수가 주어졌을 때, 8진수로 변환하는 프로그램을 작성하시오.
첫째 줄에 2진수가 주어진다. 주어지는 수의 길이는 1,000,000을 넘지 않는다.
첫째 줄에 주어진 수를 8진수로 변환하여 출력한다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
String str = sc.next();
if (str.length() % 3 == 1)
str = "00" + str;
if (str.length() % 3 == 2)
str = "0" + str;
int a = 0;
int b = 0;
int c = 0;
for (int i = str.length()-1; i >= 0; i--) {
int ch = str.charAt(i)-'0';
if (i % 3 == 2)
c = ch * 1;
else if (i % 3 == 1)
b = ch * 2;
else if (i % 3 == 0) {
a = ch * 4;
sb.append(c+b+a);
}
}
System.out.println(sb.reverse());
}
}