程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 用OC代碼判斷字符編碼格式

用OC代碼判斷字符編碼格式

編輯:C++入門知識

用OC代碼判斷字符編碼格式


總體思路:

遍歷所有的字符編碼,能正確讀取輸出轉換的就是文本文件的編碼格式。


代碼如下:

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

#import 

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;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved