实际开发当中存在一种情况:NSData转UTF-8的NSString有的时候会返回nil,原因就是UTF-8的字符中混进了其他编码格式的字符,这样NSData转NSString的时候,就是返回nil
那么我们怎么解决这一现象呢?以下是其代码实现过程:
NSData+LGJExtension.h
#import <Foundation/Foundation.h>
@interface NSData (LGJExtension)
/**
NSData转化成string
@return 返回nil的解决方案
*/
-(NSString *)convertedToUtf8String;
@end
NSData+LGJExtension.m
#import "NSData+LGJExtension.h"
@implementation NSData (LGJExtension)
- (NSString *)convertedToUtf8String{
NSString *string = [[NSString alloc] initWithData:self encoding:NSUTF8StringEncoding];
if (string == nil) {
string = [[NSString alloc] initWithData:[self UTF8Data] encoding:NSUTF8StringEncoding];
}
return string;
}
- (NSData *)UTF8Data {
//保存结果
NSMutableData *resData = [[NSMutableData alloc] initWithCapacity:self.length];
NSData *replacement = [@"�" dataUsingEncoding:NSUTF8StringEncoding];
uint64_t index = 0;
const uint8_t *bytes = self.bytes;
long dataLength = (long) self.length;
while (index < dataLength) {
uint8_t len = 0;
uint8_t firstChar = bytes[index];
// 1个字节
if ((firstChar & 0x80) == 0 && (firstChar == 0x09 || firstChar == 0x0A || firstChar == 0x0D || (0x20 <= firstChar && firstChar <= 0x7E))) {
len = 1;
}
// 2字节
else if ((firstChar & 0xE0) == 0xC0 && (0xC2 <= firstChar && firstChar <= 0xDF)) {
if (index + 1 < dataLength) {
uint8_t secondChar = bytes[index + 1];
if (0x80 <= secondChar && secondChar <= 0xBF) {
len = 2;
}
}
}
// 3字节
else if ((firstChar & 0xF0) == 0xE0) {
if (index + 2 < dataLength) {
uint8_t secondChar = bytes[index + 1];
uint8_t thirdChar = bytes[index + 2];
if (firstChar == 0xE0 && (0xA0 <= secondChar && secondChar <= 0xBF) && (0x80 <= thirdChar && thirdChar <= 0xBF)) {
len = 3;
} else if (((0xE1 <= firstChar && firstChar <= 0xEC) || firstChar == 0xEE || firstChar == 0xEF) && (0x80 <= secondChar && secondChar <= 0xBF) && (0x80 <= thirdChar && thirdChar <= 0xBF)) {
len = 3;
} else if (firstChar == 0xED && (0x80 <= secondChar && secondChar <= 0x9F) && (0x80 <= thirdChar && thirdChar <= 0xBF)) {
len = 3;
}
}
}
// 4字节
else if ((firstChar & 0xF8) == 0xF0) {
if (index + 3 < dataLength) {
uint8_t secondChar = bytes[index + 1];
uint8_t thirdChar = bytes[index + 2];
uint8_t fourthChar = bytes[index + 3];
if (firstChar == 0xF0) {
if ((0x90 <= secondChar & secondChar <= 0xBF) && (0x80 <= thirdChar && thirdChar <= 0xBF) && (0x80 <= fourthChar && fourthChar <= 0xBF)) {
len = 4;
}
} else if ((0xF1 <= firstChar && firstChar <= 0xF3)) {
if ((0x80 <= secondChar && secondChar <= 0xBF) && (0x80 <= thirdChar && thirdChar <= 0xBF) && (0x80 <= fourthChar && fourthChar <= 0xBF)) {
len = 4;
}
} else if (firstChar == 0xF3) {
if ((0x80 <= secondChar && secondChar <= 0x8F) && (0x80 <= thirdChar && thirdChar <= 0xBF) && (0x80 <= fourthChar && fourthChar <= 0xBF)) {
len = 4;
}
}
}
}
// 5个字节
else if ((firstChar & 0xFC) == 0xF8) {
len = 0;
}
// 6个字节
else if ((firstChar & 0xFE) == 0xFC) {
len = 0;
}
if (len == 0) {
index++;
[resData appendData:replacement];
} else {
[resData appendBytes:bytes + index length:len];
index += len;
}
}
return resData;
}
@end