Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decoding quoted-printables

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.

like image 786
Net Solver Avatar asked Nov 25 '11 17:11

Net Solver


People also ask

How do you encode a quoted-printable?

The format of a quoted-printable message is simple. The encoder converts any character that must be escaped to an equal sign (=) followed by the character's ASCII value in hexadecimal. For example, a VT character (ASCII value 11) is represented as =0B and a DEL character (ASCII value 127) is represented as =7F.

What does content transfer encoding quoted-printable mean?

Quoted-printable encoding is used where data is mostly US-ASCII text. It allows for 8-bit characters to be represented as their hexadecimal values. For instance, a new line can be forced by using the following string: "=0D=0A". Line lengths are limited to 76 characters.

What is quoted-printable How does it work?

Quoted-printable is an encoding method defined in the MIME standard. It is used primarily to encode 8-bit text (such as text that includes foreign characters) into 7-bit US ASCII, creating a document that is mostly readable by humans, even in its encoded form.

What is quoted-printable string?

Quoted-Printable, or QP encoding, is a binary-to-text encoding system using printable ASCII characters (alphanumeric and the equals sign = ) to transmit 8-bit data over a 7-bit data path or, generally, over a medium which is not 8-bit clean.


2 Answers

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.

like image 117
bensnider Avatar answered Sep 20 '22 01:09

bensnider


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!

like image 21
BadPirate Avatar answered Sep 20 '22 01:09

BadPirate