欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# Quoted-Printable编码、解码

程序员文章站 2022-10-26 21:12:28
复制代码 代码如下:# using system; # using system.collections; # using system.text; # # /// <...

复制代码 代码如下:

# using system;
# using system.collections;
# using system.text;
#
# /// <summary>
# /// class for encoding and decoding a string to quotedprintable
# /// rfc 1521 http://www.ietf.org/rfc/rfc1521.txt
# /// rfc 2045 http://www.ietf.org/rfc/rfc2045.txt
# /// date: 2006-03-23
# /// author: kevin spaun
# /// company: spaun informationstechnik gmbh - http://www.spaun-it.com/
# /// feedback: kspaun@spaun-it.de
# /// license: this piece of code comes with no guaranties. you can use it for whatever you want for free.
# ///
# /// modified by will huang ( http://blog.miniasp.com/post/2008/02/14/quoted-printable-encoding-and-decoding.aspx )
# /// modified at 2008-02-13
# ///
# /// modified by reterry (//www.jb51.net)
# /// 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 = string.empty;
# //byte[] bytes = encoding.default.getbytes(_toencode);
# byte[] bytes = encoding.utf8.getbytes(_toencode);
# //int count = 0;
#
# for (int i = 0; i < bytes.length; 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");
# encoded.append("=" + hex);
# }
# else
# {
# hex = bytes[i].tostring("x");
# encoded.append("=" + hex);
# }
# }
# else
# {
# //check if index out of range
# if ((i + 1) < bytes.length)
# {
# //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))
# {
# encoded.append("=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))
# {
# encoded.append("=" + bytes[i].tostring("x"));
# }
# else
# {
# encoded.append(system.convert.tochar(bytes[i]));
# }
# }
# else
# {
# encoded.append(system.convert.tochar(bytes[i]));
# }
# }
# //if (count == 75)
# //{
# // encoded.append("=\r\n"); //insert soft-linebreak
# // count = 0;
# //}
# //count++;
# }
#
# return encoded.tostring();
# }
#
# /// <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[chars.length];
#
# int bytescount = 0;
#
# for (int i = 0; i < chars.length; i++)
# {
# // if encoded character found decode it
# if (chars[i] == '=')
# {
# bytes[bytescount++] = system.convert.tobyte(int.parse(chars[i + 1].tostring() + chars[i + 2].tostring(), system.globalization.numberstyles.hexnumber));
#
# i += 2;
# }
# else
# {
# bytes[bytescount++] = system.convert.tobyte(chars[i]);
# }
# }
#
# //return system.text.encoding.default.getstring(bytes, 0, bytescount);
# return system.text.encoding.utf8.getstring(bytes, 0, bytescount);
# }
# }