C# 实现Quoted-Printable编码、解码

  1. using  System;  
  2. using  System.Collections;  
  3. using  System.Text;  
  4.   
  5. /// <summary>   
  6. /// Class for encoding and decoding a string to QuotedPrintable   
  7. /// RFC 1521 http://www.ietf.org/rfc/rfc1521.txt   
  8. /// RFC 2045 http://www.ietf.org/rfc/rfc2045.txt   
  9. /// Date: 2006-03-23   
  10. /// Author: Kevin Spaun   
  11. /// Company: SPAUN Informationstechnik GmbH - http://www.spaun-it.com/   
  12. /// Feedback: kspaun@spaun-it.de   
  13. /// License: This piece of code comes with no guaranties. You can use it for whatever you want for free.   
  14. ///   
  15. /// Modified by Will Huang ( http://blog.miniasp.com/post/2008/02/14/Quoted-Printable-Encoding-and-Decoding.aspx )   
  16. /// Modified at 2008-02-13   
  17. ///    
  18. /// Modified by yongfa365 (http://www.yongfa365.com)   
  19. /// Modified at 2008-11-29   
  20. /// Modified for MySelf   
  21. ///    
  22. /// </summary>   
  23. public   class  QuotedPrintable  
  24. {  
  25.     private   const   byte  EQUALS = 61;  
  26.     private   const   byte  CR = 13;  
  27.     private   const   byte  LF = 10;  
  28.     private   const   byte  SPACE = 32;  
  29.     private   const   byte  TAB = 9;  
  30.   
  31.     /// <summary>   
  32.     /// Encodes a string to QuotedPrintable   
  33.     /// </summary>   
  34.     /// <param name="_ToEncode">String to encode</param>   
  35.     /// <returns>QuotedPrintable encoded string</returns>   
  36.     public   static   string  Encode( string  _ToEncode)  
  37.     {  
  38.         StringBuilder Encoded = new  StringBuilder();  
  39.         string  hex =  string .Empty;  
  40.         //byte[] bytes = Encoding.Default.GetBytes(_ToEncode);   
  41.         byte [] bytes = Encoding.UTF8.GetBytes(_ToEncode);  
  42.         //int count = 0;   
  43.   
  44.         for  ( int  i = 0; i < bytes.Length; i++)  
  45.         {  
  46.             //these characters must be encoded   
  47.             if  ((bytes[i] < 33 || bytes[i] > 126 || bytes[i] == EQUALS) && bytes[i] != CR && bytes[i] != LF && bytes[i] != SPACE)  
  48.             {  
  49.                 if  (bytes[i].ToString( "X" ).Length < 2)  
  50.                 {  
  51.                     hex = "0"  + bytes[i].ToString( "X" );  
  52.                     Encoded.Append("="  + hex);  
  53.                 }  
  54.                 else   
  55.                 {  
  56.                     hex = bytes[i].ToString("X" );  
  57.                     Encoded.Append("="  + hex);  
  58.                 }  
  59.             }  
  60.             else   
  61.             {  
  62.                 //check if index out of range   
  63.                 if  ((i + 1) < bytes.Length)  
  64.                 {  
  65.                     //if TAB is at the end of the line - encode it!   
  66.                     if  ((bytes[i] == TAB && bytes[i + 1] == LF) || (bytes[i] == TAB && bytes[i + 1] == CR))  
  67.                     {  
  68.                         Encoded.Append("=0"  + bytes[i].ToString( "X" ));  
  69.                     }  
  70.                     //if SPACE is at the end of the line - encode it!   
  71.                     else   if  ((bytes[i] == SPACE && bytes[i + 1] == LF) || (bytes[i] == SPACE && bytes[i + 1] == CR))  
  72.                     {  
  73.                         Encoded.Append("="  + bytes[i].ToString( "X" ));  
  74.                     }  
  75.                     else   
  76.                     {  
  77.                         Encoded.Append(System.Convert.ToChar(bytes[i]));  
  78.                     }  
  79.                 }  
  80.                 else   
  81.                 {  
  82.                     Encoded.Append(System.Convert.ToChar(bytes[i]));  
  83.                 }  
  84.             }  
  85.             //if (count == 75)   
  86.             //{   
  87.             //    Encoded.Append("=/r/n"); //insert soft-linebreak   
  88.             //    count = 0;   
  89.             //}   
  90.             //count++;   
  91.         }  
  92.   
  93.         return  Encoded.ToString();  
  94.     }  
  95.   
  96.     /// <summary>   
  97.     /// Decodes a QuotedPrintable encoded string    
  98.     /// </summary>   
  99.     /// <param name="_ToDecode">The encoded string to decode</param>   
  100.     /// <returns>Decoded string</returns>   
  101.     public   static   string  Decode( string  _ToDecode)  
  102.     {  
  103.         //remove soft-linebreaks first   
  104.         //_ToDecode = _ToDecode.Replace("=/r/n", "");   
  105.   
  106.         char [] chars = _ToDecode.ToCharArray();  
  107.   
  108.         byte [] bytes =  new   byte [chars.Length];  
  109.   
  110.         int  bytesCount = 0;  
  111.   
  112.         for  ( int  i = 0; i < chars.Length; i++)  
  113.         {  
  114.             // if encoded character found decode it   
  115.             if  (chars[i] ==  '=' )  
  116.             {  
  117.                 bytes[bytesCount++] = System.Convert.ToByte(int .Parse(chars[i + 1].ToString() + chars[i + 2].ToString(), System.Globalization.NumberStyles.HexNumber));  
  118.   
  119.                 i += 2;  
  120.             }  
  121.             else   
  122.             {  
  123.                 bytes[bytesCount++] = System.Convert.ToByte(chars[i]);  
  124.             }  
  125.         }  
  126.   
  127.         //return System.Text.Encoding.Default.GetString(bytes, 0, bytesCount);   
  128.         return  System.Text.Encoding.UTF8.GetString(bytes, 0, bytesCount);  
  129.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值