質問

I am looking for a way to decode quoted-printables.

The quoted-printables are for arabic characters and look like this:

=D8=B3=D8=B9=D8=A7=D8=AF

I need to convert it to a string, and store it or display..

I've seen post on stackoverflow for the other way around (encoding), but couldn't find decoding.

役に立ちましたか?

解決

Uhm, it's a little hacky but you could replace the = characters with a % character and use NSString's stringByReplacingPercentEscapesUsingEncoding: method. Otherwise, you could essentially split the string on the = characters, convert each element to a byte value (easily done using NSScanner), put the byte values into a C array, and use NSString's initWithBytes:length:encoding: method.

Note that your example isn't technically in quoted-printable format, which specifies that a quoted-printable is a three character sequence consisting of an = character followed by two hex digits.

他のヒント

In my case I was coming from EML... bensnider's answer worked great... quoted-printable (at least in EML) uses an = sign followed by \r\n to signify a line wrapping, so this was the code needed to cleanly translate:

(Made as a category cause I loves dem)

@interface NSString (QuotedPrintable)
- (NSString *)quotedPrintableDecode;
@end

@implementation NSString (QuotedPrintable)
- (NSString *)quotedPrintableDecode
{
    NSString *decodedString = [self stringByReplacingOccurrencesOfString:@"=\r\n" withString:@""]; // Ditch the line wrap indicators
    decodedString = [decodedString stringByReplacingOccurrencesOfString:@"=" withString:@"%"]; // Change the ='s to %'s
    decodedString = [decodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // Replace the escaped strings.
    return decodedString;
}
@end

Which worked great for decoding my EML / UTF-8 objects!

Bensnider's answer is correct, the easy way of it.

u'll need to replace the "=" to "%"

NSString *s = @"%D8%B3%D8%B9%D8%A7%D8%AF";
NSString *s2 = [s stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

s2 stored "سعاد" which makes sense so this should work straight forward with out a hack

In some cases the line ends are not "=\r\n" but are only "=\n", in which case you need another step: decodedString = [self stringByReplacingOccurrencesOfString:@"=\n" withString:@""];

Otherwise, the final step fails due to the unbalanced "%" at the end of a line.

I know nothing of the iPhone, but most email processing libraries will contain functions to do this, as email is where this format is used. I suggest searching for MIME decoding type functions, similar to those at enter link description here.

The earlier posters approach also seems fine to me - I feel he is being a little too self-deprecating in describing it as hacky :)

Please see a working solution that takes a quoted-printable-containing strings and resolves those graphemes. The only thing you should pay attention to is the encoding (that answer is based upon UTF8, by it can be easily switched to any other): https://stackoverflow.com/a/32903103/2799410

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top