欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iOS 判断文件编码格式

程序员文章站 2022-04-03 22:25:23
...
        @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>>(%@)", arrEncodingName[i]);
                NSError *error = nil;
                NSString *filePath = pathString; // <---这里是要查看的文件路径
                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 length]>>>%d", [string length]);
                
            }  
        }