繁体   English   中英

如何使用 iOS SDK 以引用的可打印形式将 NSString 转换为 UTF?

[英]How to convert a NSString to UTF in the quoted printable form using iOS SDK?

我需要将一个字符串(可能带有非拉丁字符)转换为 UTF-8,进一步编码为带引号的可打印形式

例如,我有一个字符串,例如“привет”,我需要将其转换为“=D0=BF=D1=80.......”之类的东西。

有人可以帮助我吗?

上面的答案确实有效,但它们对整个字符串进行了编码。 这通常不是您想要的,特别是如果您将此代码用于编写 email 之类的事情。

您实际上可能追求的是一种仅在 QP 中对那些非 ascii 字符进行编码的方法。

输入:

Simply place the bass—that's the whole bass—into the Bass-o-matic.

Output(天真):

=53=69=6D=70=6C=79=20=70=6C=61=63=65=20=74=68=65=20=62=61=73=73=E2=80=94=74=68=61=74=E2=80=99=73=20=74=68=65=20=77=68=6F=6C=65=20=62=61=73=73=E2=80=94=69=6E=74=6F=20=74=68=65=20=42=61=73=73=2D=6F=2D=6D=61=74=69=63=2E

Output(改进):

Simply place the bass=E2=80=94that=E2=80=99s the whole bass=E2=80=94into the Bass-o-matic.

更像这样的东西:

- (NSString *)qpEncodedStringWithString:(NSString *)string {
    NSCharacterSet *asciiSet = [NSCharacterSet characterSetWithRange:NSMakeRange(0, 127)];
    NSCharacterSet *invertedAscii = [asciiSet invertedSet];

    // if the string contains non-ascii characters, we need to encode them
    // otherwise, we'll just return the string below
    if ([string rangeOfCharacterFromSet:invertedAscii].location != NSNotFound) {

        // because the % sign is, itself, an ascii character, we must first replace it
        // with a placeholder unlikely to occur in a string, but still within ascii
        NSString *percentPlaceholder = @"QqQxXxPpP";
        string = [string stringByReplacingOccurrencesOfString:@"%" withString:percentPlaceholder];

        // use Apple's method to percent encode the string
        string = [string stringByAddingPercentEncodingWithAllowedCharacters:asciiSet];

        // replace those percents with = signs
        string = [string stringByReplacingOccurrencesOfString:@"%" withString:@"="];

        // and restore the true percent symbols
        string = [string stringByReplacingOccurrencesOfString:percentPlaceholder withString:@"%"];
    }

    return string;
}

(建立在这个答案的基础上,从bensnider解码QP 字符串)

好吧,我不会遵循这种编码的所有规范。 假设你没有拖尾空格、换行符和其他不好的东西,这里是你需要的代码:

@interface NSString(QuotedPrintable)

- (NSString *)quotedPrintable;

@end

@implementation NSString(QuotedPrintable)

- (NSString *)quotedPrintable {
    const char *utfString = [self UTF8String];
    char *p = utfString;
    NSMutableString *result = [NSMutableString stringWithString:@""];

    while (*p) {
        char chars[2];
        char c = *p;
        for (int i = 0; i < 2; i++) {
            int val = ((int)c & 15);
            if (val < 10) {
                chars[i] = '0'+val;
            } else {
                chars[i] = 'A'+(val-10);
            }
            c = c >> 4;
        }
        [result appendFormat:@"=%c%c", chars[1], chars[0]];
        p++;
    }
    return [NSString stringWithString:result];
}

@end

对于привет它给出=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82

这与安东的回答几乎相同,只是更短:

- (NSString *)quotedPrintableString
    NSMutableString *encoded = [NSMutableString stringWithCapacity:3*[self length]];
    const char *characters = [self UTF8String];
    NSUInteger length = strlen(characters);
    for (NSUInteger i = 0; i < length; ++i) {
        char character = characters[i];
        int left = character & 0xF;
        int right = (character >> 4) & 0xF;
        [encoded appendFormat:@"=%X%X", right, left];
    }
    return encoded;
}

用这个 -

NSString *yourString = @"Hello world";
const char *utfString = [yourString UTF8String];
NSSTring* finalEncodedString = [NSString stringWithUTF8String:utfString];

现在打印“finalEncodedString”,它现在是 UTF8 编码的字符串。

试试[NSString stringWithUTF8String:yourString];

NSString *yourString = @"Hello world";
char *utfString = [yourString UTF8String];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM