본문 바로가기

Programming Language/Objective-C

NSString과 C 문자열간 변환하기

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Foundation에서 제공하는 NSString 문자열을 C 문자열로 변환하기 위해서는 -cStringUsingEncoding: 메시지를 사용하면 된다. 반대로 C 문자열을 NSString로 변환하여 쓰기 위해서는 +stringWithCString:encoding:을 사용한다.

메시지 원형

- (const char *) cStringUsingEncoding : (NSStringEncoding) encoding
+ (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc
  • encoding

    const char * 형 버퍼가 갖는 데이터의 인코딩 종류를 지정한다. (아래 참조)

사용 예

#import <Foundation/Foundation.h>
#import <stdio.h>
#import <stdlib.h>
#import <string.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

	char szBuffer1[512];
	char szBuffer2[512];
	NSString * nsString = nil;
	const char * cString = NULL;
	
	printf("write any string : ");
	scanf("%s", szBuffer1);
	fflush(stdin);
	
	nsString = [NSString stringWithCString: szBuffer1 encoding: NSASCIIStringEncoding];
	cString = [nsString cStringUsingEncoding: NSASCIIStringEncoding];
	strcpy(szBuffer2, cString);
	
	printf("szBuffer1: %s\n", szBuffer1);
	NSLog(@"nsString: %@\n", nsString);
	printf("szBuffer2: %s\n", szBuffer2);
	
	printf("END");
	
    [pool drain];
    return 0;
}

더 보기

인코딩 목록 (출처: http://blog.marines.co.kr/archives/946)

상수 값상수명설명인코딩명
1NSASCIIStringEncoding Western (ASCII) US-ASCII
2NSNEXTSTEPStringEncoding Western (NextStep) X-NEXTSTEP
3NSJapaneseEUCStringEncoding Japanese (EUC) EUC-JP
4NSUTF8StringEncoding Unicode (UTF-8) UTF-8
5NSISOLatin1StringEncoding Western (ISO Latin 1) ISO-8859-1
6NSSymbolStringEncoding Symbol (Mac OS) X-MAC-SYMBOL
7NSNonLossyASCIIStringEncoding Non-lossy ASCII
8NSShiftJISStringEncoding Japanese (Windows, DOS) CP932
9NSISOLatin2StringEncoding Central European (ISO Latin 2) ISO-8859-2
10NSUnicodeStringEncoding Unicode (UTF-16) UTF-16
11NSWindowsCP1251StringEncoding Cyrillic (Windows) WINDOWS-1251
12NSWindowsCP1252StringEncoding Western (Windows Latin 1) WINDOWS-1252
13NSWindowsCP1253StringEncoding Greek (Windows) WINDOWS-1253
14NSWindowsCP1254StringEncoding Turkish (Windows Latin 5) WINDOWS-1254
15NSWindowsCP1250StringEncoding Central European (Windows Latin 2) WINDOWS-1250
21NSISO2022JPStringEncoding Japanese (ISO 2022-JP) ISO-2022-JP
30NSMacOSRomanStringEncoding Western (Mac OS Roman) MACINTOSH
-1677721344
Unicode (UTF-32LE) UTF-32LE
-1744830208
Unicode (UTF-32BE) UTF-32BE
-1811939072
Unicode (UTF-16LE) UTF-16LE
-1879047936
Unicode (UTF-16BE) UTF-16BE
-1946156800
Unicode (UTF-32) UTF-32
-2147480574
Western (EBCDIC Latin 1) IBM037
-2147481079
Traditional Chinese (Big 5-E)
-2147481080
Ukrainian (KOI8-U) KOI8-U
-2147481082
Traditional Chinese (Big 5 HKSCS) Big5-HKSCS
-2147481083
Simplified Chinese (HZ GB 2312) HZ-GB-2312
-2147481084
Western (Mac Mail) X-MAC-ROMAN-LATIN1
-2147481085
Traditional Chinese (Big 5) BIG5
-2147481086
Cyrillic (KOI8-R) KOI8-R
-2147481087
Japanese (Shift JIS) SHIFT_JIS
-2147481280
Korean (EUC) EUC-KR
-2147481295
Traditional Chinese (EUC) EUC-TW
-2147481296
Simplified Chinese (EUC) EUC-CN
-2147481536
Korean (ISO 2022-KR) ISO-2022-KR
-2147482062
Chinese (GB 18030) GB18030
-2147482063
Chinese (GBK) GBK
-2147482072
Japanese (Shift JIS X0213) Shift_JIS
-2147482360
Vietnamese (Windows) WINDOWS-1258
-2147482361
Baltic Rim (Windows) WINDOWS-1257
-2147482362
Arabic (Windows) WINDOWS-1256
-2147482363
Hebrew (Windows) WINDOWS-1255
-2147482589
Traditional Chinese (Windows DOS), CP950
-2147482590
Korean (Windows DOS), CP949
-2147482591
Simplified Chinese (Windows DOS), CP936
-2147482595
Thai (Windows DOS), CP874
-2147482596
Greek (DOS Greek 2) IBM869
-2147482597
Cyrillic (DOS) CP866
-2147482598
Nordic (DOS)
-2147482599
Arabic (DOS) cp864
-2147482600
Canadian French (DOS)
-2147482601
Hebrew (DOS) DOS-862
-2147482602
Icelandic (DOS) CP861
-2147482603
Portuguese (DOS)
-2147482604
Turkish (DOS) CP857
-2147482605
Cyrillic (DOS)
-2147482606
Central European (DOS Latin 2) CP852
-2147482607
Greek (DOS Greek 1)
-2147482608
Western (DOS Latin 1) CP850
-2147482618
Baltic Rim (DOS) CP775
-2147482619
Greek (DOS) CP737
-2147482624
Latin-US (DOS) CP437
-2147483120
Romanian (ISO Latin 10) ISO-8859-16
-2147483121
Western (ISO Latin 9) ISO-8859-15
-2147483122
Celtic (ISO Latin 8) ISO-8859-14
-2147483123
Baltic Rim (ISO Latin 7) ISO-8859-13
-2147483125
Thai (ISO 8859-11) ISO-8859-11
-2147483126
Nordic (ISO Latin 6) ISO-8859-10
-2147483127
Turkish (ISO Latin 5) ISO-8859-9
-2147483128
Hebrew (ISO 8859-8) ISO-8859-8
-2147483129
Greek (ISO 8859-7) ISO-8859-7
-2147483130
Arabic (ISO 8859-6) ISO-8859-6
-2147483131
Cyrillic (ISO 8859-5) ISO-8859-5
-2147483132
Central European (ISO Latin 4) ISO-8859-4
-2147483133
Western (ISO Latin 3) ISO-8859-3
-2147483412
Inuit (Mac OS)
-2147483496
Cyrillic (Mac OS Ukrainian) X-MAC-UKRAINIAN
-2147483508
Farsi (Mac OS) X-MAC-FARSI
-2147483607
Keyboard Symbols (Mac OS)
-2147483608
Gaelic (Mac OS)
-2147483609
Celtic (Mac OS)
-2147483610
Romanian (Mac OS) X-MAC-ROMANIAN
-2147483611
Icelandic (Mac OS) X-MAC-ICELANDIC
-2147483612
Croatian (Mac OS) X-MAC-CROATIAN
-2147483613
Turkish (Mac OS) X-MAC-TURKISH
-2147483614
Dingbats (Mac OS) X-MAC-DINGBATS
-2147483619
Central European (Mac OS) X-MAC-CENTRALEURROMAN
-2147483622
Tibetan (Mac OS) X-MAC-TIBETAN
-2147483623
Simplified Chinese (Mac OS) X-MAC-SIMP-CHINESE
-2147483627
Thai (Mac OS) X-MAC-THAI
-2147483637
Gujarati (Mac OS) X-MAC-GUJARATI
-2147483638
Gurmukhi (Mac OS) X-MAC-GURMUKHI
-2147483639
Devanagari (Mac OS) X-MAC-DEVANAGARI
-2147483641
Cyrillic (Mac OS) X-MAC-CYRILLIC
-2147483642
Greek (Mac OS) X-MAC-GREEK
-2147483643
Hebrew (Mac OS) X-MAC-HEBREW
-2147483644
Arabic (Mac OS) X-MAC-ARABIC
-2147483645
Korean (Mac OS) X-MAC-KOREAN
-2147483646
Traditional Chinese (Mac OS) X-MAC-TRAD-CHINESE
-2147483647
Japanese (Mac OS) X-MAC-JAPANESE