写了一个二维码,闸机读出来竟然不对,我就问你尴不尴尬?因为我二维码传的是原始的字符串,读出来之后是303232333435等,这分明是要反加密的节奏啊,于是utf-8反加密,当数据中每个字节全部小于80时完全正常无压力。但是当数据大于80时就加了c2 80或c3 80,c2 90 或c3 90.这就更尴尬了。找了半天没有丝毫头绪。就知道可能是加密方式错了,可是变换各种加密方式无果。整整一天发现生成二维码中使用了utf8编码。需要改成我们需要的ISO-8859-1即NSISOLatin1StringEncoding编码。它最大支持FF(255),而ACSCII最大支持127.
下面是我搜集的编码范围,希望朋友们可以帮我补全。这些编码格式都是什么时机用到。
NSArray *arrayEncoding = @[@(NSASCIIStringEncoding),/* 0..127 only */
@(NSNEXTSTEPStringEncoding),
@(NSJapaneseEUCStringEncoding),
@(NSUTF8StringEncoding),/*UTF-8有点类似于Haffman编码,它将Unicode编码为00000000-0000007F的字符,用单个字节来表示;
00000080-000007FF的字符用两个字节表示
00000800-0000FFFF的字符用3字节表示
因为目前为止Unicode-16规范没有指定FFFF以上的字符,所以UTF-8最多是使用3个字节来表示一个字符。但理论上来说,UTF-8最多需要用6字节表示一个字符*/
@(NSISOLatin1StringEncoding),/*通过 iso-8859-1 转化为字符串时,将直接得到 [0x00D6, 0x00D0] 两个 UNICODE 字符,即 "ÖÐ"。反之,将 UNICODE 字符串通过 iso-8859-1 转化为字节串时,只能正常转化 0~255 范围的字符。*/
@(NSSymbolStringEncoding),
@(NSNonLossyASCIIStringEncoding),
@(NSShiftJISStringEncoding),
@(NSISOLatin2StringEncoding),
@(NSUnicodeStringEncoding),/*它是一种2字节编码,能够提供65536个字符,如"A"的Unicode编码为6500,而BigEndianUnicode编码为0065*/
@(NSWindowsCP1251StringEncoding),
@(NSWindowsCP1252StringEncoding),
@(NSWindowsCP1253StringEncoding),
@(NSWindowsCP1254StringEncoding),
@(NSWindowsCP1250StringEncoding),
@(NSISO2022JPStringEncoding),
@(NSMacOSRomanStringEncoding),
@(NSUTF16StringEncoding),
@(NSUTF16BigEndianStringEncoding),
@(NSUTF16LittleEndianStringEncoding),
@(NSUTF32StringEncoding),
@(NSUTF32BigEndianStringEncoding),
@(NSUTF32LittleEndianStringEncoding)
];
另附简单的文件的编码格式分析,结果肯定是一大墩:看看就行
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
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 *imagePath = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"rtf"];
NSString *filePath = imagePath; // <---这里是要查看的文件路径
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]);
}
}