λ°μν
νλ‘κ·Έλλ¨Έμ€ λ€μ ν° μ«μ λ¬Έμ λ₯Ό νλ μ€ μ§μ λ³ννλ κ²μ λν΄μ μ°Ύμλ³΄κ² λλ€.
10μ§μλ₯Ό 2, 8, 16μ§μλ‘ λ³ννλ λ°©λ²
int decimal = 17;
String binary = Integer.toBinaryString(decimal); // 10μ§μ → 2μ§μ
String octal = Integer.toOctalString(decimal); // 10μ§μ → 8μ§μ
String hexaDecimal = Integer.toHexString(decimal); // 10μ§μ → 16μ§μ
10μ§μλ₯Ό 2μ§μλ‘ λ°κΎΈκ³ μΆλ€λ©΄
Integer.toBinaryString(decimal);
μ΄λ κ² μ΄λ€.
λ°ν κ° νμμ Stringμ΄λ€.
2, 8, 16μ§μλ₯Ό 10μ§μλ‘ λ³ννκΈ°
int binary2 = Integer.parseInt(binary, 2);
int octal2 = Integer.parseInt(octal, 8);
int hexaDecimal2 = Integer.parseInt(hexaDecimal, 16);
Integer.parseInt(binary, 2);
첫 λ²μ§Έ μΈμ : λ°κΎΈκ³ μ νλ μ«μ λ£κΈ°
λ λ²μ§Έ μΈμ : 첫 λ²μ§Έ μΈμκ° λͺ μ§μμΈμ§ λ£κΈ°
첫 λ²μ§Έ μΈμλ Stringλ§ λ£μ μ μλ€. (μ«μλ λ£μ μ μλ€.)
2μ§μλ₯Ό 10μ§μλ‘ λ³ννλ μ½λ
int num16 = Integer.parseInt("10111", 2);
int num17 = Integer.parseInt("1010101", 2);
bw.write(String.valueOf(num16) + "\n"); // 23
bw.write(String.valueOf(num17)); // 85
μ΄ μ½λ
package algorithm;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int decimal = 17;
String binary = Integer.toBinaryString(decimal); // 10μ§μ → 2μ§μ
String octal = Integer.toOctalString(decimal); // 10μ§μ → 8μ§μ
String hexaDecimal = Integer.toHexString(decimal); // 10μ§μ → 16μ§μ
bw.write("10μ§μ :" + String.valueOf(decimal) + "\n");
bw.write("2μ§μ :" + binary + "\n");
bw.write("8μ§μ :" + octal + "\n");
bw.write("16μ§μ :" + hexaDecimal + "\n");
int binary2 = Integer.parseInt(binary, 2);
int octal2 = Integer.parseInt(octal, 8);
int hexaDecimal2 = Integer.parseInt(hexaDecimal, 16);
bw.write("λ€μ 10μ§μλ‘ : " + binary2 + "\n");
bw.write("λ€μ 10μ§μλ‘ : " + octal2 + "\n");
bw.write("λ€μ 10μ§μλ‘ : " + hexaDecimal2 + "\n");
bw.flush();
bw.close();
}
}
λ°μν