UTF-7 编码转换

#import "GTMBase64.h"

//utf-7  需要用到GTMBase64

 

- (NSString *)stringFromUTF7String:(NSString *)utf7String

{

    NSMutableString *result = [NSMutableString string];

    NSInteger location = 0;

    NSInteger len = 0;

    BOOL isBase64 = NO;

    for (int i = 0; i < utf7String.length; i++) {

        unichar ch = [utf7String characterAtIndex:i];

        if (ch == '&') {

            location = i +1;

            isBase64 = YES;

            continue;

        }

        if (ch == '-') {

            //            location = i +1;

            isBase64 = NO;

            NSString *base64 = [utf7String substringWithRange:NSMakeRange(location, len)];

            [result appendString:[self decodeBase64:base64]];

            len = 0;

            location = 0;

            continue;

        }

        if (isBase64) {

            len++;

        } else {

            [result appendString:[NSString stringWithCharacters:&ch length:1]];

            

        }

        

        

    }

    return result;

}

- (NSString *)decodeBase64:(NSString *)base64String

{

    //    NSString *unicode = @"T2BZfQ";//19 54 1 25 31 16

    

    NSData *data = [GTMBase64 webSafeDecodeString:base64String];

    NSString *result = [[NSString alloc] initWithData:data encoding:NSUnicodeStringEncoding];

    

    return result;

}

你可能感兴趣的:(编码转换)