decoding quoted-printables
Asked Answered
P

6

8

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.

Pendergast answered 25/11, 2011 at 17:45 Comment(1)
Here's a NSString category that parses MIME Encoded-Words with quote-printables: github.com/hpique/NSString-MimeEncodedWordGreco
S
10

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.

Sunlight answered 25/11, 2011 at 18:7 Comment(2)
the example is in quoted-printable, but was missing an = in the beginning: =D8=B3=D8=B9=D8=A7=D8=AFPendergast
Hmm, I actually think my first case is less hacky than I imagined it. I had assumed that it wouldn't catch an errant = character in the body, but apparently = characters have to be qp encoded as well. So yeah...should work.Sunlight
T
3

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!

Tarkington answered 4/6, 2012 at 21:20 Comment(0)
P
2

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

Prediction answered 15/2, 2012 at 3:0 Comment(0)
G
2

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.

Godbeare answered 24/9, 2012 at 21:15 Comment(0)
P
1

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

Pugilism answered 14/2, 2012 at 12:57 Comment(0)
A
0

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://mcmap.net/q/1323890/-decoding-quoted-printable-messages-in-swift

Adrian answered 2/10, 2015 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.