Converting NSString to Base64 Data for XML Serialization

前端 未结 1 943
轻奢々
轻奢々 2020-12-07 03:07

I use Apple\'s NSXMLParser class to load data. I have a separate framework for serializing my data.

I\'ve had enough of the long dash, the Word quotes, etc. showing

相关标签:
1条回答
  • 2020-12-07 03:16

    Got it. I'm using the encoding/decoding methods from this answer to convert my NSString objects to NSData objects, and vice versa: Any base64 library on iphone-sdk?

    And then I wrote these quick methods that make use of the above methods, and allow me to write the Base64 string data to XML. Loading it up works great, has been tested with Chinese characters, Word characters, etc. You can also parse these out if you want, but at least the error isn't in the parser. (Which could easily result in data loss if you don't handle the errors properly.)

    + (NSString *)toBase64String:(NSString *)string {
        NSData *data = [string dataUsingEncoding: NSUnicodeStringEncoding];
    
        NSString *ret = [NSStringUtil base64StringFromData:data length:[data length]];
    
        return ret;
    }
    
    + (NSString *)fromBase64String:(NSString *)string {
        NSData  *base64Data = [NSStringUtil base64DataFromString:string];
    
        NSString* decryptedStr = [[NSString alloc] initWithData:base64Data encoding:NSUnicodeStringEncoding];
    
        return [decryptedStr autorelease];
    }
    

    Edit: As the original link is down, I hunted down my code from a while ago, here are my NSStringUtil methods mentioned above. Note I did not author this code, but it has been working well for years:

    + (NSData *)base64DataFromString: (NSString *)string {
        unsigned long ixtext, lentext;
        unsigned char ch, input[4], output[3];
        short i, ixinput;
        Boolean flignore, flendtext = false;
        const char *temporary;
        NSMutableData *result;
    
        if (!string) {
            return [NSData data];
        }
    
        ixtext = 0;
    
        temporary = [string UTF8String];
    
        lentext = [string length];
    
        result = [NSMutableData dataWithCapacity: lentext];
    
        ixinput = 0;
    
        while (true) {
            if (ixtext >= lentext) {
                break;
            }
    
            ch = temporary[ixtext++];
    
            flignore = false;
    
            if ((ch >= 'A') && (ch <= 'Z')) {
                ch = ch - 'A';
            } else if ((ch >= 'a') && (ch <= 'z')) {
                ch = ch - 'a' + 26;
            } else if ((ch >= '0') && (ch <= '9')) {
                ch = ch - '0' + 52;
            } else if (ch == '+') {
                ch = 62;
            } else if (ch == '=') {
                flendtext = true;
            } else if (ch == '/') {
                ch = 63;
            } else {
                flignore = true; 
            }
    
            if (!flignore) {
                short ctcharsinput = 3;
                Boolean flbreak = false;
    
                if (flendtext) {
                    if (ixinput == 0) {
                        break;
                    }
    
                    if ((ixinput == 1) || (ixinput == 2)) {
                        ctcharsinput = 1;
                    } else {
                        ctcharsinput = 2;
                    }
    
                    ixinput = 3;
    
                    flbreak = true;
                }
    
                input[ixinput++] = ch;
    
                if (ixinput == 4) {
                    ixinput = 0;
    
                    unsigned char0 = input[0];
                    unsigned char1 = input[1];
                    unsigned char2 = input[2];
                    unsigned char3 = input[3];
    
                    output[0] = (char0 << 2) | ((char1 & 0x30) >> 4);
                    output[1] = ((char1 & 0x0F) << 4) | ((char2 & 0x3C) >> 2);
                    output[2] = ((char2 & 0x03) << 6) | (char3 & 0x3F);
    
                    for (i = 0; i < ctcharsinput; i++) {
                        [result appendBytes: &output[i] length: 1];
                    }
                }
    
                if (flbreak) {
                    break;
                }
            }
        }
    
        return result;
    }
    
    + (NSString *)base64StringFromData: (NSData *)data length: (NSUInteger)length {
        unsigned long ixtext, lentext;
        long ctremaining;
        unsigned char input[3], output[4];
        short i, charsonline = 0, ctcopy;
        const unsigned char *raw;
        NSMutableString *result;
    
        lentext = [data length];
    
        if (lentext < 1) {
            return @"";
        }
    
        result = [NSMutableString stringWithCapacity: lentext];
    
        raw = [data bytes];
    
        ixtext = 0;
    
        while (true) {
            ctremaining = lentext - ixtext;
    
            if (ctremaining <= 0) {
                break;
            }
    
            for (i = 0; i < 3; i++) { 
                unsigned long ix = ixtext + i;
    
                if (ix < lentext) {
                    input[i] = raw[ix];
                } else {
                    input[i] = 0;
                }
            }
    
            output[0] = (input[0] & 0xFC) >> 2;
            output[1] = ((input[0] & 0x03) << 4) | ((input[1] & 0xF0) >> 4);
            output[2] = ((input[1] & 0x0F) << 2) | ((input[2] & 0xC0) >> 6);
            output[3] = input[2] & 0x3F;
    
            ctcopy = 4;
    
            switch (ctremaining) {
                case 1: 
                    ctcopy = 2;
                    break;
                case 2: 
                    ctcopy = 3;
                    break;
            }
    
            for (i = 0; i < ctcopy; i++) {
                [result appendString: [NSString stringWithFormat: @"%c", base64EncodingTable[output[i]]]];
            }
    
            for (i = ctcopy; i < 4; i++) {
                [result appendString: @"="];
            }
    
            ixtext += 3;
            charsonline += 4;
    
            if ((ixtext % 90) == 0) {
                [result appendString: @"\n"];
            }
    
            if (length > 0) {
                if (charsonline >= length) {
                    charsonline = 0;
    
                    [result appendString: @"\n"];
                }
            }
        }
    
        return result;
    }
    
    0 讨论(0)
提交回复
热议问题