decoding quoted-printables

对着背影说爱祢 提交于 2019-12-18 07:44:32

问题


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.


回答1:


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.




回答2:


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!




回答3:


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




回答4:


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.




回答5:


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 :)




回答6:


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



来源:https://stackoverflow.com/questions/8272607/decoding-quoted-printables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!