decoding quoted-printables

前端 未结 6 1373
伪装坚强ぢ
伪装坚强ぢ 2020-12-20 16:40

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=

相关标签:
6条回答
  • 2020-12-20 17:20

    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

    0 讨论(0)
  • 2020-12-20 17:20

    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.

    0 讨论(0)
  • 2020-12-20 17:23

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

    0 讨论(0)
  • 2020-12-20 17:26

    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.

    0 讨论(0)
  • 2020-12-20 17:36

    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

    0 讨论(0)
  • 2020-12-20 17:37

    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!

    0 讨论(0)
提交回复
热议问题