SoFunction
Updated on 2025-03-07

C# Quoted-Printable encoding and decoding


# using System;
# using ;
# using ;
#
# /// <summary>
# /// Class for encoding and decoding a string to QuotedPrintable
# /// RFC 1521 /rfc/
# /// RFC 2045 /rfc/
# /// Date: 2006-03-23
# /// Author: Kevin Spaun
# /// Company: SPAUN Informationstechnik GmbH - /
# /// Feedback: kspaun@
# /// License: This piece of code comes with no guaranties. You can use it for whatever you want for free.
# ///
# /// Modified by Will Huang ( /post/2008/02/14/ )
# /// Modified at 2008-02-13
# ///
# /// Modified by reterry (https://)
# /// Modified at 2008-11-29
# /// Modified for MySelf
# ///
# /// </summary>
# public class QuotedPrintable
# {
# private const byte EQUALS = 61;
# private const byte CR = 13;
# private const byte LF = 10;
# private const byte SPACE = 32;
# private const byte TAB = 9;
#
# /// <summary>
# /// Encodes a string to QuotedPrintable
# /// </summary>
# /// <param name="_ToEncode">String to encode</param>
# /// <returns>QuotedPrintable encoded string</returns>
# public static string Encode(string _ToEncode)
# {
# StringBuilder Encoded = new StringBuilder();
# string hex = ;
# //byte[] bytes = (_ToEncode);
# byte[] bytes = Encoding.(_ToEncode);
# //int count = 0;
#
# for (int i = 0; i < ; i++)
# {
# //these characters must be encoded
# if ((bytes[i] < 33 || bytes[i] > 126 || bytes[i] == EQUALS) && bytes[i] != CR && bytes[i] != LF && bytes[i] != SPACE)
# {
# if (bytes[i].ToString("X").Length < 2)
# {
# hex = "0" + bytes[i].ToString("X");
# ("=" + hex);
# }
# else
# {
# hex = bytes[i].ToString("X");
# ("=" + hex);
# }
# }
# else
# {
# //check if index out of range
# if ((i + 1) < )
# {
# //if TAB is at the end of the line - encode it!
# if ((bytes[i] == TAB && bytes[i + 1] == LF) || (bytes[i] == TAB && bytes[i + 1] == CR))
# {
# ("=0" + bytes[i].ToString("X"));
# }
# //if SPACE is at the end of the line - encode it!
# else if ((bytes[i] == SPACE && bytes[i + 1] == LF) || (bytes[i] == SPACE && bytes[i + 1] == CR))
# {
# ("=" + bytes[i].ToString("X"));
# }
# else
# {
# ((bytes[i]));
# }
# }
# else
# {
# ((bytes[i]));
# }
# }
# //if (count == 75)
# //{
# // ("=\r\n"); //insert soft-linebreak
# // count = 0;
# //}
# //count++;
# }
#
# return ();
# }
#
# /// <summary>
# /// Decodes a QuotedPrintable encoded string
# /// </summary>
# /// <param name="_ToDecode">The encoded string to decode</param>
# /// <returns>Decoded string</returns>
# public static string Decode(string _ToDecode)
# {
# //remove soft-linebreaks first
# //_ToDecode = _ToDecode.Replace("=\r\n", "");
#
# char[] chars = _ToDecode.ToCharArray();
#
# byte[] bytes = new byte[];
#
# int bytesCount = 0;
#
# for (int i = 0; i < ; i++)
# {
# // if encoded character found decode it
# if (chars[i] == '=')
# {
# bytes[bytesCount++] = ((chars[i + 1].ToString() + chars[i + 2].ToString(), ));
#
# i += 2;
# }
# else
# {
# bytes[bytesCount++] = (chars[i]);
# }
# }
#
# //return (bytes, 0, bytesCount);
# return .(bytes, 0, bytesCount);
# }
# }