Quoted-Printable编码/解码c#类代码

在参照上篇文章《QuotedPrintable编码c#类代码》中给出的QuotedPrintable编码的c#类代码后,本篇将给出QuotedPrintable解码的C#类代码,该代码已经在VS.NET2003下编译并测试通过,现将编解码类和测试程序给出,仅供参考,不足处请读者指出。

QuotedPrintable编码/解码类:

using  System.Text;
using  System.IO;
using  System;
namespace  NoPaste
{
    
public class QuotedPrintable
    
{
        
/// <summary>
        
/// QuotedPrintable解码函数
        
/// </summary>
        
/// <param name="input">需要解码的QuotedPrintable字符串</param>
        
/// <returns>解码后的字符串</returns>

        public static string Decode(string input)
        
{
            System.Text.Encoding encoding 
= System.Text.Encoding.GetEncoding("gb2312");

            StringBuilder result 
= new StringBuilder();
            StringReader sr 
= new StringReader(input);

            
string line = sr.ReadLine();

            
while( line!=null )
            
{
                
bool addCRLF = true;
                
byte[] bytes = System.Text.Encoding.ASCII.GetBytes( line.ToCharArray() );

                
for(int i=0;i<bytes.Length;i++)
                
{
                    
if( ( bytes[i]>=33 && bytes[i]<=60 ) || ( bytes[i]>=62 && bytes[i]<=126 ) || bytes[i]==9 || bytes[i]==32)
                    
{
                        result.Append( Convert.ToChar( bytes[i] ) );
                        
continue;
                    }

                    
else if( bytes[i]==61 )
                    
{
                        
if( i==bytes.Length-1 )
                        
{
                            
//eg. = soft line break;
                            addCRLF = false;
                            
break;
                        }

                        
else if( bytes[i+1]==51 && bytes[i+2]==68 )
                        
{
                            
//eg. =3D
                            i++;i++;
                            result.Append( 
"=" );
                            
continue;
                        }

                        
else
                        
{
                            
//eg. =B7=D6
                            byte[] b = new byte[2];
                            b[
0= Convert.ToByte( Convert.ToChar(bytes[i+1]).ToString()+Convert.ToChar(bytes[i+2]).ToString(),16 );
                            i
++;i++;i++;
                            b[
1= Convert.ToByte( Convert.ToChar(bytes[i+1]).ToString()+Convert.ToChar(bytes[i+2]).ToString(),16 );
                            i
++;i++;

                            result.Append(encoding.GetString(b));
                            
continue;
                        }


                    }

                }
//end of for

                line 
= sr.ReadLine();
                
if( line!=null && addCRLF )
                    result.Append(
" ");    
            }
//end of while
            return result.ToString();
        }


        
public static string Encode(string input)
        
{
            System.Text.Encoding encoding 
= System.Text.Encoding.GetEncoding("gb2312");
            
const int MAXLINELENGTH = 76;
            
int currentLineLength = 0;
            
byte[] bytes = encoding.GetBytes(input.ToCharArray());
            StringBuilder result 
= new StringBuilder();

            
for (int i = 0; i < bytes.Length; i++)
            
{
                
if (bytes[i] == 10 || bytes[i] == 13)
                
{
                    
if (bytes[i] == 13 && GetNextByte(i, bytes, 1== 10)
                    
{
                        CheckLineLength(MAXLINELENGTH, 
ref currentLineLength, 0, result);
                        result.Append(
" ");
                        currentLineLength 
= 0;
                        i
++;
                        
continue;
                    }


                    
if (bytes[i] == 10)
                    
{
                        CheckLineLength(MAXLINELENGTH, 
ref currentLineLength, 0, result);
                        result.Append(
" ");
                        currentLineLength 
= 0;
                    }


                    
if (bytes[i] == 13)
                    
{
                        CheckLineLength(MAXLINELENGTH, 
ref currentLineLength, 3, result);
                        result.Append(
"=" + ConvertToHex(bytes[i]));
                    }

                }

                
else
                
{
                    
if ((bytes[i] >= 33 && bytes[i] <= 60|| (bytes[i] >= 62 && bytes[i] <= 126))
                    
{
                        CheckLineLength(MAXLINELENGTH, 
ref currentLineLength, 1, result);
                        result.Append(System.Convert.ToChar(bytes[i]));
                    }

                    
else
                    
{
                        
if (bytes[i] == 9 || bytes[i] == 32)
                        
{
                            CheckLineLength(MAXLINELENGTH, 
ref currentLineLength, 0, result);
                            result.Append(System.Convert.ToChar(bytes[i]));
                            currentLineLength
++;
                        }

                        
else
                        
{
                            CheckLineLength(MAXLINELENGTH, 
ref currentLineLength, 3, result);
                            result.Append(
"=" + ConvertToHex(bytes[i]));
                        }

                    }

                }

            }


            
return result.ToString();
        }


        
private static void CheckLineLength(int maxLineLength, ref int currentLineLength, int newStringLength, StringBuilder sb)
        
{
            
if (currentLineLength + 1 == maxLineLength || currentLineLength + newStringLength + 1 >= maxLineLength)
            
{
                sb.Append(
"= ");
                currentLineLength 
= 0+newStringLength;
            }

            
else
            
{
                currentLineLength 
+= newStringLength;
            }

        }


        
private static int GetNextByte(int index, byte[] bytes, int shiftValue)
        
{
            
int newIndex = index + shiftValue;

            
if (newIndex < 0 || newIndex > bytes.Length - 1 || bytes.Length == 0)
                
return -1;
            
else
                
return bytes[newIndex];
        }


        
private static string ConvertToHex(byte number)
        
{
            
string result = System.Convert.ToString(number, 16).ToUpper();
            
return (result.Length == 2? result : "0" + result;
        }

    }

}


 

测试程序代码:

        [STAThread]
        
static   void  Main( string [] args)
        
{
            
string init_str = "CSDN.NET - 中国最大的IT技术社区!";
            
string encode_str = NoPaste.QuotedPrintable.Encode( init_str );
            
string decode_str = NoPaste.QuotedPrintable.Decode( encode_str );

            Console.WriteLine(
                
"需要进行QuotedPrintable编码的原始字符串: "+
                
"    {0} "+
                
"编码后得到的字符串: "+
                
"    {1} "+
                
"对编码后得到的字符串进行解码得到的字符串: "+
                
"    {2} ",init_str,encode_str,decode_str);
            
        }

测试程序运行输出结果:

 

Quoted-Printable编码/解码c#类代码_第1张图片

你可能感兴趣的:(String,C#,测试,null,input,byte)