美文网首页
解决NSData转化成string,返回nil的问题

解决NSData转化成string,返回nil的问题

作者: 邓布利多教授 | 来源:发表于2019-05-10 09:40 被阅读0次
    • 返回nil的解决方案

    1、创建一个NSData的分类

    • .h文件
    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface NSData (LYDataCategory)
    
    /**
     NSData转化成string
     
     @return 返回nil的解决方案
     */
    -(NSString *)convertedToUtf8String;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    • .m文件
    #import "NSData+LYDataCategory.h"
    
    @implementation NSData (LYDataCategory)
    
    -(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
    

    2、调用

    在用到的地方或者全局文件里引用:#import "NSData+LYDataCategory.h”
    在需要转化地方调用:NSString *dataString = [anyData convertedToUtf8String];
    

    最后感谢写这篇文章的老铁,可惜我当时忘记了复制原文链接,抱歉了……

    相关文章

      网友评论

          本文标题:解决NSData转化成string,返回nil的问题

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