Encode text as QuotedPrintable

Adam D. Ruppe destructionator at gmail.com
Wed Nov 20 09:04:06 PST 2013


On Wednesday, 20 November 2013 at 16:53:47 UTC, Andre wrote:
> is there a library function for encoding text as 
> QuotedPrintable?

I fixed the function in that post:


char[] generateQuotedPrintable(in char[] string, in char[] 
charset =
"UTF-8")
{
	char[] output;
	bool special = false;

	char[] toHex(char value)
	{
		char[2] buffer;

		ubyte a = value % 16, b = value / 16;

		buffer[0] = cast(char)(a < 10 ? a + '0' : a - 10 + 'A');
		buffer[1] = cast(char)((b < 10) ? b + '0' : b - 10 + 'A');

		return buffer.dup;
	}

	for (int i = 0; i < string.length; i++)
	{
		if (string[i] < 128 && string[i] != '=' && string[i] != '?' &&
string[i] != '_')
			output ~= string[i];
		else
		{
			output ~= "=" ~ toHex(string[i]);
			special = true;
		}
	}

	if (special)
		output = "=?" ~ charset ~ "?Q?" ~ output ~ "?=";

	return output;
}



There were two problems: one is a cast was needed due to a 
language change since it was written that is now more strict 
about narrowing conversions, and the other is it said i +=2 and i 
don't know why it did that and am pretty sure it is just wrong, 
so I removed that.

This function should work now.

If you need more email related stuff, I wrote a file that can 
send html email and such and can also read mbox messages:

https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/email.d


More information about the Digitalmars-d-learn mailing list