iphone里读取gb2312(中文)编码文件或者二进制流(转)

1。如何读取UTF-8编码的文本文件?

1 NSString *filePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:fileName];
2     [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil] 
3     //当然也可以采用如下方法
4     //NSData *data = [NSData dataWithContentsOfFile:filePath];
5     //NSString *textFile  = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

2。如何读取GB2312(中文)的文本文件?

 1 /* Note that in addition to the values explicitly listed below, NSStringEncoding supports encodings provided by CFString.
 2 See CFStringEncodingExt.h for a list of these encodings.
 3 See CFString.h for functions which convert between NSStringEncoding and CFStringEncoding.
 4 */
 5 enum {
 6     NSASCIIStringEncoding = 1,  /* 0..127 only */
 7     NSNEXTSTEPStringEncoding = 2,
 8     NSJapaneseEUCStringEncoding = 3,
 9     NSUTF8StringEncoding = 4,
10     NSISOLatin1StringEncoding = 5,
11     NSSymbolStringEncoding = 6,
12     NSNonLossyASCIIStringEncoding = 7,
13     NSShiftJISStringEncoding = 8,          /* kCFStringEncodingDOSJapanese */
14     NSISOLatin2StringEncoding = 9,
15     NSUnicodeStringEncoding = 10,
16     NSWindowsCP1251StringEncoding = 11,    /* Cyrillic; same as AdobeStandardCyrillic */
17     NSWindowsCP1252StringEncoding = 12,    /* WinLatin1 */
18     NSWindowsCP1253StringEncoding = 13,    /* Greek */
19     NSWindowsCP1254StringEncoding = 14,    /* Turkish */
20     NSWindowsCP1250StringEncoding = 15,    /* WinLatin2 */
21     NSISO2022JPStringEncoding = 21,        /* ISO 2022 Japanese encoding for e-mail */
22     NSMacOSRomanStringEncoding = 30,
23 
24     NSUTF16StringEncoding = NSUnicodeStringEncoding,      /* An alias for NSUnicodeStringEncoding */
25 
26 #if MAC_OS_X_VERSION_10_4 <= MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_2_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
27     NSUTF16BigEndianStringEncoding = 0x90000100,          /* NSUTF16StringEncoding encoding with explicit endianness specified */
28     NSUTF16LittleEndianStringEncoding = 0x94000100,       /* NSUTF16StringEncoding encoding with explicit endianness specified */
29 
30     NSUTF32StringEncoding = 0x8c000100,                   
31     NSUTF32BigEndianStringEncoding = 0x98000100,          /* NSUTF32StringEncoding encoding with explicit endianness specified */
32     NSUTF32LittleEndianStringEncoding = 0x9c000100        /* NSUTF32StringEncoding encoding with explicit endianness specified */
33 #endif
34 };

很遗憾,我没找到,不过Apple留了个线索让我取找,不过仔细看看它的注释

Note that in addition to the values explicitly listed below, NSStringEncoding supports encodings provided by CFString.

See CFStringEncodingExt.h for a list of these encodings.

See CFString.h for functions which convert between NSStringEncoding and CFStringEncoding.

我的英文比较不好,但大意是看明白了,不在下面支持了编码格式,在CFStringEncodingExt.h 里头文件申明 。

我们通过finder的文件查找方法找到CFStringEncodingExt.h 。

仔细看看确实找到了kCFStringEncodingGB_18030_2000 ( 我以为是kCFStringEncodingGB_2312_80 ,实际上不是) 但是这个是CFStringEncoding类型,我们需要NSEncode的类型。

CFString 和NSString具有相同的内存结构,也是NSString的重要补充,通过查找CFString的帮助文档,找到了这个方法 CFStringConvertEncodingToNSStringEncoding

  NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
  NSString *textFile  = [NSString stringWithContentsOfFile:filePath encoding:enc error:nil];

 那么第二个问题就解决了 

posted @ 2012-05-08 18:06  妙笔  阅读(333)  评论(0编辑  收藏  举报