Swift & iOS
[Swift & iOS] Unicode 코드 변환 관련
jkkooooooo
2021. 11. 19. 00:04
반응형
파이썬에서는 간단하게 ord 내장함수를 사용 하여 문자를 유니코드로 변환을 해줍니다
Swift 에서는 어떻게 하는지 알아보겠습니다
- Unicode to Int
let str: String = "a" let number: Int = Int(UnicodeScalar(str)!.value) or let number: Int = Int(UnicodeScalar("a").value) print(number) // 97 (Uint32)
UnicodeScalar 함수에 변환 하고 싶은 문자를 넣으면 UInt32 타입의 값의 아스키코드를 얻을 수 있습니다 - Int to Unicode
1번 방법과는 반대로 UnicodeScalar 함수에 아스키코드 값을 넣으면 원하는 값을 얻을 수 있습니다.let unicode: String = String(UnicodeScalar(97)!) print(unicode) // "a"
끝!!!!
반응형