10 진수 → N 진수
사용법
- number.toString(N)
let number = 32;
let binary = number.toString(2) // 100000
let octal = number.toString(8) // 40
let hex = number.toString(16) // 20
N 진수 → 10 진수
사용법
- parseInt(number, N)
- 여기서 number는 각 N진수에 해당하는 숫자입니다. ex) 2진법이면 number = 1010
let decimal = parseInt(100000, 2); // 32
let decimal = parseInt(40, 8); // 32
let decimal = parseInt(20, 16); // 32
N 진수 → M 진수
사용법
- N 진수 → 10진수 → M 진수 의 순서로 변환해주면 됩니다.
- N 진수를 먼저 10진수로 변환합니다. parseInt(number, N)
- 이어서 만들어진 10진수를 M 진수로 변환합니다. number.toString(M)
16 진수를 2진수로 바꾸는 과정
let hex = 32;
let decimal = parseInt(hex, 16) // 먼저 16진수를 10진수로 변환합니다.
let result = decimal.toString(2) // 10진수를 2진수로 변환합니다.
'Programing > JavaScript' 카테고리의 다른 글
[JavaScript] 호이스팅이란? (1) | 2025.04.15 |
---|---|
[JavaScript] 호출한 객체를 가리키는 this 용법 (0) | 2024.04.02 |
[JS] 배열의 요소를 정렬하는 sort() 함수 (0) | 2023.12.25 |
[JS] 배열 함수 - reduce() 사용법 (0) | 2023.08.22 |
댓글