코딩하는 나귀

[출처 : http://incn.tistory.com/category/프로그램?page=6]

- (NSString *)GetUTF8String:(NSString *)hanggulString {

NSArray *chosung = [[NSArray allocinitWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil];

NSArray *jungsung = [[NSArray allocinitWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil];

NSArray *jongsung = [[NSArray allocinitWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@" ",@"",@"",nil];

NSString *textResult = @"";

for (int i=0;i<[hanggulString length];i++) {

NSInteger code = [hanggulString characterAtIndex:i];

if (code >= 44032 && code <= 55203) {

NSInteger uniCode = code - 44032;

NSInteger chosungIndex = uniCode / 21 / 28;

NSInteger jungsungIndex = uniCode % (21 * 28) / 28;

NSInteger jongsungIndex = uniCode % 28;

textResult = [NSString stringWithFormat:@"%@%@%@%@", textResult, [chosung objectAtIndex:chosungIndex], [jungsung objectAtIndex:jungsungIndex], [jongsung objectAtIndex:jongsungIndex]];

}

}

return textResult;

}


<사용예>

NSLog@"%@", [self GetUTF8String:@"투덜이"]);