Overall idea:
Traverse the entire character encoding. The correct way to read the output conversion is the text file encoding format.
The code is as follows:
main.m//detect text character encoding format tips////Created by Chieh XI (duzixi) on 14-6-21.//Copyright (c) 2014 lanou3g.com All rights rese rved.//#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), @ (Nswindowscp1252stringencodi NG), @ (nswindowscp1253stringencoding), @ (nswindowscp1254stringencoding), @ (nswindowscp1250stringencoding), @ (nsiso2022jpstringencoding), @ (nsmacosromanstringencoding), @ (nsutf16stringencoding), @ (nsutf16bigendianstringencoding), @ (nsutf16littleendianstring Encoding), @ (nsutf32stringencoding), @ (nsutf32bigendianstr ingencoding), @ (nsutf32littleendianstringencoding)]; Nsarray *arrencodingname = @[@ "nsasciistringencoding", @ "Nsnextstepstringe Ncoding ", @" nsjapaneseeucstringencoding ", @" NSUTF8 Stringencoding ", @ "nsisolatin1stringencoding", @ "nssymbolstringencoding", @ "nsnonlossyasciistringencoding", @ "Nsshiftjisstrin Gencoding ", @" nsisolatin2stringencoding ", @" Nsunic Odestringencoding ", @" nswindowscp1251stringencoding ", @ "nswindowscp1252stringencoding", @ "nswindowscp1253stringencoding", @ "nswindowscp1254stringencoding", @ "Nswindowscp1250stringencodin G ", @" nsiso2022jpstringencoding ", @" Nsmacosromanst Ringencoding ", @" nsutf16stringencoding ", @" NSUTF16 Bigendianstringencoding ", @ "nsutf16littleendianstringencoding", @ "nsutf32s Tringencoding ", @" nsutf32bigendianstringencoding ", @ "Nsutf32littleendianstringencoding"]; for (int i = 0; i < [arrencoding count]; i++) {unsigned long encodingcode = [Arrencoding[i] Unsignedlongva Lue]; NSLog (@ "(%@)", Arrencodingname[i]); Nserror *error = nil; NSString *filepath = @ "/users/dlios1/desktop/event.xml"; <---Here is the file path to view nsstring *astring = [nsstring stringwithcontentsoffile:filepath encoding:encodingcode ER ror:&error]; NSLog (@ "error:%@", [Error localizeddescription]); NSData *data = [astring Datausingencoding:encodingcode]; NSString *string = [[NSString alloc] Initwithdata:data Encoding:encodingcode]; NSLog (@ "%@", String); /*//if necessary, you can also create a file and then test [string writetofile:[nsstring stringwithformat:@]/users/dlios1/desktop/%@. XML ", Arrencodingname[i]] Atomically:yes Encoding:encodingcode error:&error]; */}} return 0;}
How does "IOS" infer the character encoding format of a text file