美文网首页
NSData转NSString为nil的解决方法

NSData转NSString为nil的解决方法

作者: 皮蛋豆腐酱油 | 来源:发表于2019-10-11 10:42 被阅读0次
    #import "NSData+UTF8.h"
    
    @implementation NSData (UTF8)
    
    - (NSString *)utf8String {
        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;
    }
    

    参考的连接找不到了。。。

    相关文章

      网友评论

          本文标题:NSData转NSString为nil的解决方法

          本文链接:https://www.haomeiwen.com/subject/bkktmctx.html