How does "IOS" infer the character encoding format of a text file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.