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

发布于 2024-11-29 07:02:33 字数 213 浏览 1 评论 0原文

我需要将字符串(可能包含非拉丁字符)转换为 UTF-8,并在 引用中进一步编码-可打印表格

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

有人可以帮助我吗?

I need to convert a string (probably with non latin characters) to UTF-8, further encoded in a quoted-printable form.

For example I have a string, e.g "привет" and I need to convert it to "=D0=BF=D1=80......." something like that.

Can somebody help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

末蓝 2024-12-06 07:02:33

这与 安东的回答,只是短得多:

- (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;
}

This is pretty much the same thing as Anton's answer, just a lot shorter:

- (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;
}
毅然前行 2024-12-06 07:02:33

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

@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

Well, I won't follow all the specs of this encoding. Assuming that you don't have tailing whitespaces, newline character and other bad stuff, here is the code you need:

@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

For привет it gives =D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82

思念满溢 2024-12-06 07:02:33

上面的答案确实有效,但它们对整个字符串进行编码。这通常不是您想要的,特别是当您使用此代码进行撰写电子邮件之类的操作时。

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

输入:

只需将低音(即整个低音)放入 Bass-o-matic。

输出(简单):

<代码>=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

输出(改进):

只需将 bass=E2=80=94that=E2=80=99s 整个 bass=E2=80=94 放入 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;
}

:(基于这个答案构建,用于解码来自本斯尼德

The above answers do work, but they encode the whole string. Which isn't usually what you'd want, particularly if you're using this code for something like composing an email.

What you're actually likely after is a method to encode only those non-ascii characters in QP.

Input:

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

Output (naiive):

=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 (improved):

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

Something more like this:

- (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;
}

(builds off of this answer for decoding QP strings from bensnider)

满身野味 2024-12-06 07:02:33

尝试 [NSString stringWithUTF8String:yourString];

Try [NSString stringWithUTF8String:yourString];

z祗昰~ 2024-12-06 07:02:33
NSString *yourString = @"Hello world";
char *utfString = [yourString UTF8String];
NSString *yourString = @"Hello world";
char *utfString = [yourString UTF8String];
魂归处 2024-12-06 07:02:33

使用这个 -

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

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

Use This -

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

Now print the 'finalEncodedString', it's UTF8 encoded string now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文