码迷,mamicode.com
首页 > 移动开发 > 详细

【iOS】怎样推断文本文件的字符编码格式

时间:2015-12-24 19:24:42      阅读:406      评论:0      收藏:0      [点我收藏+]

标签:

整体思路:

遍历全部的字符编码。能正确读取输出转换的就是文本文件的编码格式。


代码例如以下:

//
//  main.m
//  检測文本字符编码格式的小技巧
//
//  Created by 杜子兮 (duzixi) on 14-6-21.
//  Copyright (c) 2014年 lanou3g.com All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        NSArray *arrEncoding = @[@(NSASCIIStringEncoding),
                                 @(NSNEXTSTEPStringEncoding),
                                 @(NSJapaneseEUCStringEncoding),
                                 @(NSUTF8StringEncoding),
                                 @(NSISOLatin1StringEncoding),
                                 @(NSSymbolStringEncoding),
                                 @(NSNonLossyASCIIStringEncoding),
                                 @(NSShiftJISStringEncoding),
                                 @(NSISOLatin2StringEncoding),
                                 @(NSUnicodeStringEncoding),
                                 @(NSWindowsCP1251StringEncoding),
                                 @(NSWindowsCP1252StringEncoding),
                                 @(NSWindowsCP1253StringEncoding),
                                 @(NSWindowsCP1254StringEncoding),
                                 @(NSWindowsCP1250StringEncoding),
                                 @(NSISO2022JPStringEncoding),
                                 @(NSMacOSRomanStringEncoding),
                                 @(NSUTF16StringEncoding),
                                 @(NSUTF16BigEndianStringEncoding),
                                 @(NSUTF16LittleEndianStringEncoding),
                                 @(NSUTF32StringEncoding),
                                 @(NSUTF32BigEndianStringEncoding),
                                 @(NSUTF32LittleEndianStringEncoding)
                                 ];
        
        NSArray *arrEncodingName = @[@"NSASCIIStringEncoding",
                                     @"NSNEXTSTEPStringEncoding",
                                     @"NSJapaneseEUCStringEncoding",
                                     @"NSUTF8StringEncoding",
                                     @"NSISOLatin1StringEncoding",
                                     @"NSSymbolStringEncoding",
                                     @"NSNonLossyASCIIStringEncoding",
                                     @"NSShiftJISStringEncoding",
                                     @"NSISOLatin2StringEncoding",
                                     @"NSUnicodeStringEncoding",
                                     @"NSWindowsCP1251StringEncoding",
                                     @"NSWindowsCP1252StringEncoding",
                                     @"NSWindowsCP1253StringEncoding",
                                     @"NSWindowsCP1254StringEncoding",
                                     @"NSWindowsCP1250StringEncoding",
                                     @"NSISO2022JPStringEncoding",
                                     @"NSMacOSRomanStringEncoding",
                                     @"NSUTF16StringEncoding",
                                     @"NSUTF16BigEndianStringEncoding",
                                     @"NSUTF16LittleEndianStringEncoding",
                                     @"NSUTF32StringEncoding",
                                     @"NSUTF32BigEndianStringEncoding",
                                     @"NSUTF32LittleEndianStringEncoding"
                                    ];
        
        for (int i = 0 ; i < [arrEncoding count]; i++) {
            unsigned long encodingCode = [arrEncoding[i] unsignedLongValue];
            NSLog(@"(%@)", arrEncodingName[i]);
            NSError *error = nil;
            NSString *filePath = @"/Users/dlios1/Desktop/event.xml"; // <---这里是要查看的文件路径
            NSString *aString = [NSString stringWithContentsOfFile:filePath encoding:encodingCode  error:&error];
            NSLog(@"Error:%@", [error localizedDescription]);
            NSData *data = [aString dataUsingEncoding:encodingCode];
            NSString *string = [[NSString alloc] initWithData:data encoding:encodingCode];
            NSLog(@"%@", string);
            
            /*
            // 假设有必要,还能够把文件创建出来再測试
            [string writeToFile:[NSString stringWithFormat:@"/Users/dlios1/Desktop/%@.xml", arrEncodingName[i]]
                     atomically:YES
                       encoding:encodingCode
                          error:&error];
             */
        }
    }
    return 0;
}


【iOS】怎样推断文本文件的字符编码格式

标签:

原文地址:http://www.cnblogs.com/gcczhongduan/p/5074002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!