투케이2K

19. (swift/xcode) base64 encode 인코딩 , decode 디코딩 수행 실시 본문

Swift

19. (swift/xcode) base64 encode 인코딩 , decode 디코딩 수행 실시

투케이2K 2021. 10. 14. 08:47
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : SWIFT


[소스 코드]

// [extension 정의 실시 : String]
extension String {
    func base64Encoded() -> String? { // base64 인코딩 수행 실시
        if let data = self.data(using: .utf8) {
            return data.base64EncodedString()
        }
        return ""
    }

    func base64Decoded() -> String? { // base64 디코딩 수행 실시
        if let data = Data(base64Encoded: self, options: .ignoreUnknownCharacters) {
            return String(data: data, encoding: .utf8)
        }
        return ""
    }
}

 

 

 

     /*
     [요약 설명]
     1. base64 : 8bit의 데이터(바이너리)를 6bit의 크기로 표현한 것입니다
     2. base64Encoded : base64 인코딩을 수행합니다
     3. base64Decoded : base64 디코딩을 수행합니다
     */
    
    
    
    // [테스트 메인 함수 정의 실시]
    func testMain() {
        print("[Program Start]")
        print("")
        
        // base64 인코딩 수행 실시
        let base64Encode = "hello".base64Encoded() ?? "" // [??] 대체값 지정 optional 추출
        print("base64Encode : " , base64Encode)
        print("")
        
        
        // base64 디코딩 수행 실시
        let base64Decode = base64Encode.base64Decoded() ?? "" // [??] 대체값 지정 optional 추출
        print("base64Decode : " , base64Decode)
        print("")
    }

[결과 출력]


[요약 설명]

/*

[요약 설명]

1. base64 : 8bit의 데이터(바이너리)를 6bit의 크기로 표현한 것입니다

2. base64Encoded : base64 인코딩을 수행합니다

3. base64Decoded : base64 디코딩을 수행합니다

*/


 

반응형
Comments