Thread overview
Encode text as QuotedPrintable
Nov 20, 2013
Andre
Nov 20, 2013
Adam D. Ruppe
Nov 20, 2013
Andre
November 20, 2013
Hi,

is there a library function for encoding text as QuotedPrintable?
I found an old post from 2005:
http://forum.dlang.org/thread/cv98ti$1sf8$1@digitaldaemon.com

The used function toHex throws errors. I also not found
an alternative way to convert a char to a hex as replacement
for the toHex function.

Kind regards
André
November 20, 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
November 20, 2013
On Wednesday, 20 November 2013 at 17:04:07 UTC, Adam D. Ruppe
wrote:
> 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[] ...
>
> 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

Thanks a lot for the fix. Also the file is great.

Kind regards
André