C # Quoted-Printable encoding and decoding

Source: Internet
Author: User

Copy codeThe Code is as follows: # 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 (http://www.jb51.net)
# // Modified
# // 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 );
#}
#}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.