Overall Thinking:
Traverse all character encodings and read and convert the encoding format of text files.
The Code is as follows:
//// Main. M // tips for detecting text character encoding formats /// 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), @ (optional ), @ (nsutf8stringencoding), @ (response), @ (nsshiftjisstringencoding), @ (response), @ (nsunicodestringencoding), @ (response ), @ (random), @ (random), @ (nswindowscp1254stringencoding), @ (random), @ (random), @ (nsmacosromanstringencoding), @ (nsutf16stringencoding), @ (random ), @ (encoding), @ (nsutf32stringencoding), @ (encoding), @ (encoding)]; nsarray * arrencodingname = @ [@ "nsasciistringencoding", @ "nsnextstepstringencoding ", @ "Telecom", @ "nsutf8stringencoding", @ "Telecom", @ "nsshiftjisstringencoding", @ "Telecom", @ "nsunicodestringencoding ", @ "example", @ "example", @ "nswindowscp1253stringencoding", @ "example", @ "nsmacosromanstringencoding", @ "nsutf16stringencoding ", @ "random", @ "random", @ "nsutf32stringencoding", @ "nsutf32bigendianstringencoding", @ "random"]; 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 "; // <--- Here is the file path 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);/* // if necessary, you can create a file and then test [String writetofile: [nsstring stringwithformat: @ "/users/dlios1/desktop/% @. XML ", arrencodingname [I] atomically: Yes encoding: encodingcode error: & error]; */} return 0 ;}
Use OC code to determine the character encoding format