투케이2K

129. (Objective-C/objc) char to data 데이터 형 변환 수행 실시 - unsigned char , NSData 본문

Objective-C

129. (Objective-C/objc) char to data 데이터 형 변환 수행 실시 - unsigned char , NSData

투케이2K 2022. 10. 29. 14:48
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : OBJECTIVE-C

 

[소스 코드]

// MARK: - [헤더 파일에 정의 없이 : void 메소드 구현]
- (void)testMain {
    printf("\n");
    printf("==================================== \n");
    printf("[ViewController >> testMain() :: 테스트 메소드 수행] \n");
    printf("==================================== \n");
    printf("\n");

    
    // [try catch 구문 정의 실시]
    @try {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            // [초기 변수 선언 실시]
            const char *charData = "hello";
            NSData *bytes = [NSData dataWithBytes:charData length:strlen(charData)];
            
            unsigned char *convertChar = (unsigned char *)[bytes bytes];
            
            
            // [로그 출력 실시]
            NSLog(@"==================================== \n");
            NSLog(@"[ViewController >> testMain() :: NSLog 로그 결과 확인 실시] \n");
            NSLog(@"==================================== \n");
            NSLog(@"[charData :: %s] \n", charData);
            NSLog(@"==================================== \n");
            NSLog(@"[bytes :: %@] \n", bytes);
            NSLog(@"==================================== \n");
            NSLog(@"[convertChar :: %s] \n", convertChar);
            NSLog(@"==================================== \n");

        });

    }
    @catch (NSException *exception) {
        printf("\n");
        printf("==================================== \n");
        printf("[ViewController >> catch :: 예외 상황 확인] \n");
        printf("[name :: %s] \n", exception.name.description.UTF8String);
        printf("[reason :: %s] \n", exception.reason.description.UTF8String);
        printf("==================================== \n");
        printf("\n");
    }
}
 

[결과 출력]

 

 

반응형
Comments