본문 바로가기
iOS/swift

진법 변환

by 패쓰킴 2022. 8. 31.
728x90

Radix

https://developer.apple.com/documentation/swift/int/init(_:radix:)

문자열이나 기수에서 integer 값을 만들어 준다.

10진수 -> 2진수

let num = 78
print(String(num, radix: 2)) // 1001110


2진수 -> 10진수

let num = "1001110"
print(Int(num, radix: 2)!) // 78

 

nonzeroBitCount

https://developer.apple.com/documentation/swift/fixedwidthinteger/nonzerobitcount

이진값에서 1의 개수를 알려준다.

let num = 78
print(num.nonzeroBitCount) // 4
728x90

'iOS > swift ' 카테고리의 다른 글

숫자 다루기  (1) 2023.11.07
attributedString  (0) 2022.12.26
Array  (0) 2022.12.21
String  (0) 2022.09.22
Alamofire timeout set(Swift5)  (0) 2022.08.25

댓글