나름 프로그래밍?/ Objective-C
NSString 한글 2바이트로 계산..
-Dong-
2011. 7. 14. 23:16
아이폰은 한글이 3바이트 라서..
NSInteger convertedLength = [targetStr lengthOfBytesUsingEncoding:(0x80000000 + 0x0422)]; //EUC-KR 식으로 길이 계산
char *converted = [targetStr cStringUsingEncoding:(0x80000000 + 0x0422)]; //EUC-KR 형식으로 인코딩된 char*
NSLog(@"%d 글자 입력", convertedLength);
if (convertedLength > 40) { // 한글은 2바이트로 계산한 Byte 길이 체크
NSData *data = [NSData dataWithBytes:target length:40]; // 딱 거기까지 잘라오기
NSString *convertedStr = [[NSString alloc] initWithData:data encoding:(0x80000000 + 0x0422)]; // 다시 변환 ㅠㅠ
targetStr = convertedStr; //변환된거 저장
[convertedStr release];
}
더 좋은 방법이 있을텐데..............
NSInteger convertedLength = [targetStr lengthOfBytesUsingEncoding:(0x80000000 + 0x0422)]; //EUC-KR 식으로 길이 계산
char *converted = [targetStr cStringUsingEncoding:(0x80000000 + 0x0422)]; //EUC-KR 형식으로 인코딩된 char*
NSLog(@"%d 글자 입력", convertedLength);
if (convertedLength > 40) { // 한글은 2바이트로 계산한 Byte 길이 체크
NSData *data = [NSData dataWithBytes:target length:40]; // 딱 거기까지 잘라오기
NSString *convertedStr = [[NSString alloc] initWithData:data encoding:(0x80000000 + 0x0422)]; // 다시 변환 ㅠㅠ
targetStr = convertedStr; //변환된거 저장
[convertedStr release];
}
더 좋은 방법이 있을텐데..............