CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

谁有Quoted-Printable算法的函数

楼主nik_Amis(...)2005-11-11 12:15:35 在 .NET技术 / C# 提问

我在收邮件的时候,附件名字是被Quoted-Printable过的  
  变成这种字符串了:=?iso-8859-1?q?=B6=AB=C0=BC=BF=C6=BC=BC=B7=A2=D5=B9=D3=D0=CF=DE=B9=AB=CB=BE_=C3=E6=CA=D4=CA=D4=CC=E2.doc?=  
  谁有这个算法的函数  
  public   static   string   qp(string   Raw)  
   
  问题点数:50、回复次数:7Top

1 楼dejollia(Jollia)回复于 2005-11-11 12:45:17 得分 15

Quoted-Printable简称QP,   一般用在Email系统中。它通常用于少量文本方式的8位字符的编码,这种编码的应该是很好辨认的,它有大量的"="。下面是它的一个例子:  
          QP的算法可以说是最简单的也可以说是编码效率很低的一种编码(它的编码率是1:3),它是专门为了处理8位字符制定的。  
    它的算法是:读一个字符,如果ASCII码大于127,即字符的第8位是1的话,进行编码,否则忽略(有时也对7位字符编码)。编码很简单,看下面的C语言描述即可,关于QP的详细说明和准确定义可以参阅RFC2045。  
          /*   QP编码   */  
          void   qp(unsigned   char   sour,   unsigned   char&   first,   unsigned   char&   second)  
          /*  
          sour:要编码的字符  
          first:编码后的第一个字符  
          second:编码后的第二个字符  
          first和second为返回值  
          */  
          {  
          if(sour   >   127)  
          {  
                  first=sour   >>   4;  
                  second=sour   &   15;  
                  if(first   >   9)  
                          first   +=   55;  
                  else  
                          first   +=   48;  
                  if(second   >   9)  
                          second   +=   55;  
                  else  
                          second   +=   48;  
                  printf("%c%c%c",   '=',   first,   second);  
          }  
          }  
   
          /*QP解码*/  
          void   uqp(unsigned   char&   sour,   unsigned   char   first,   unsigned   char   second)  
          /*  
          sour:解码后的字符  
          first:QP码的第一个字符  
          second:QP码的第二个字符  
          sour为返回值  
          */  
          {  
                  if(first   >=   65)  
                          first   -=   55;  
                  else  
                          first   -=   48;  
                  if(second   >=   65)  
                          second   -=   55;  
                  else  
                          second   -=   48;  
                  sour   =   NULL;  
                  sour   =   first<<4;  
                  sour   |=   second;  
          }Top

2 楼nik_Amis(...)回复于 2005-11-11 13:38:25 得分 0

这个偶也搜到了,那位大虾翻译成C#?  
  Top

3 楼purpo(漂泊的云)回复于 2005-11-30 10:45:47 得分 0

东兰科技发展有限公司_面试试题.doc  
  Top

4 楼purpo(漂泊的云)回复于 2005-12-01 15:35:34 得分 0

using   System;  
  using   System.IO;  
  using   System.Text;  
  using   System.Text.RegularExpressions;  
  using   System.Security;  
  using   System.Collections;  
   
   
  namespace   WindowsApplication1  
  {  
  ///   QuotedPrintable   的摘要说明。  
  ///   Quoted-Printable也是MIME邮件中常用的编码方式之一。  
  ///   同Base64一样,它也将输入的字符串或数据编码成全是ASCII码的可打印字符串。    
   
  ///Quoted-Printable编码的基本方法是:输入数据在33-60、62-126范围内的,直接输出;  
  ///其它的需编码为“=”加两个字节的HEX码(大写)。  
  ///为保证输出行不超过规定长度,可在行尾加“=\r\n”序列作为软回车。    
   
  public   class   QuotedPrintable  
  {  
  private   QuotedPrintable()  
  {  
  }  
   
  public   const   int   RFC_1521_MAX_CHARS_PER_LINE   =   75;  
   
   
  public   static   string   Encode(string   toencode)  
  {  
  return   Encode(toencode,   RFC_1521_MAX_CHARS_PER_LINE);  
  }  
   
  public   static   string   Encode(string   toencode,   int   charsperline)  
  {  
  if   (toencode   ==   null)  
  throw   new   ArgumentNullException();  
   
  if   (charsperline   <=   0)  
  throw   new   ArgumentOutOfRangeException();  
   
  string   line,   encodedHtml   =   "";  
  StringReader   sr   =   new   StringReader(toencode);  
  try  
  {  
  while((line=sr.ReadLine())!=null)  
  encodedHtml   +=   EncodeSmallLine(line);  
   
  return   FormatEncodedString(encodedHtml,   charsperline);  
  }  
  finally  
  {  
  sr.Close();  
  sr   =   null;  
  }  
  }  
   
  public   static   string   EncodeFile(string   filepath)  
  {  
  return   EncodeFile(filepath,   RFC_1521_MAX_CHARS_PER_LINE);  
  }  
   
   
  public   static   string   EncodeFile(string   filepath,   int   charsperline)  
  {  
  if   (filepath   ==   null)  
  throw   new   ArgumentNullException();  
   
  string   encodedHtml   =   "",   line;  
  FileInfo   f   =   new   FileInfo(filepath);  
   
  if   (!   f.Exists)  
  throw   new   FileNotFoundException();  
   
  StreamReader   sr   =   f.OpenText();  
  try  
  {  
  while((line=sr.ReadLine())!=null)  
  encodedHtml   +=   EncodeSmallLine(line);  
   
  return   FormatEncodedString(encodedHtml,   charsperline);  
  }  
  finally  
  {  
  sr.Close();  
  sr   =   null;  
  f   =   null;  
  }  
  }  
   
   
  public   static   string   EncodeSmall(string   s)  
  {  
  if   (s   ==   null)  
  throw   new   ArgumentNullException();  
   
  string   result   =   "";  
  byte[]   pByte   =   Encoding.Default.GetBytes(s);  
  for(   int   i   =   0   ;   i   <=   pByte.Length-1   ;i++)  
  {  
  if   (   (pByte[i]   >=   33   &&   pByte[i]   <=   60   )   ||   (   pByte[i]   >=62   &&   pByte[i]   <=126   ))  
  {  
  result   +=   (char)pByte[i]   ;  
   
  }  
  else  
  {  
  result   =   result   +"="   +pByte[i].ToString("X2");  
  }  
   
  }  
  return   result   ;  
   
  }  
  public   static   string   EncodeSmallLine(string   s)  
  {  
  if   (s   ==   null)  
  throw   new   ArgumentNullException();  
   
  return   EncodeSmall(s   +   "\r\n");  
  }  
   
  public     static   string   FormatEncodedString(string   qpstr,   int   maxcharlen)  
  {  
  if   (qpstr   ==   null)  
  throw   new   ArgumentNullException();  
   
  string   strout   =   "";  
  StringWriter   qpsw   =   new   StringWriter();  
  try  
  {  
  byte[]   pByte   =   Encoding.Default.GetBytes(qpstr);  
  for(int   i   =   0   ;i   <=   pByte.Length   -   1   ;i++)  
  {  
  strout   +=   (char)pByte[i]   ;  
  if   (   i   ==   maxcharlen   )  
  {  
  qpsw.WriteLine("{0}=",   strout);  
  qpsw.Flush();  
   
  i=0;  
  strout   =   "";  
  }  
   
  }  
  qpsw.WriteLine(strout);  
  qpsw.Flush();  
  return   qpsw.ToString();  
  }  
  finally  
  {  
  qpsw.Close();  
  qpsw   =   null;  
  }  
   
  }  
  static   string   HexDecoder(string   line)  
  {  
   
   
  string   result   =   "";  
  string   tempLine   =   "";  
  if   (   line.EndsWith("=")   )  
  {  
  tempLine   =   line.Substring(0,line.Length-1)   ;  
  }  
  else  
  {  
  tempLine   =   line   ;  
   
  }  
  char[]   pChar   =   tempLine.ToCharArray();  
  ArrayList   al   =   new   ArrayList();  
   
   
   
  for(int   i   =   0;i<=   pChar.Length-1;i++   )  
  {  
  if(   pChar[i]   ==   '='   )  
  {  
  al.Add(int.Parse(pChar[i+1].ToString()+pChar[i+2].ToString(),System.Globalization.NumberStyles.HexNumber));  
  i   =   i   +   2;  
   
  }  
  else  
  {  
  al.Add((int)pChar[i]);  
   
  }  
  }  
  byte[]   pByte   =   new   byte[al.Count];  
  for(   int   i   =   0;i   <=   pByte.Length-1;i++)  
  {  
  tempLine   =   al[i].ToString();  
  pByte[i]   =   byte.Parse(tempLine);  
  }  
  result   =   Encoding.Default.GetString(pByte);  
  return   result;  
  }  
   
   
  public   static   string   DecodeFile(string   filepath)  
  {  
  if   (filepath   ==   null)  
  throw   new   ArgumentNullException();  
   
  string   decodedHtml   =   "",   line;  
  FileInfo   f   =   new   FileInfo(filepath);  
   
  if   (!   f.Exists)  
  throw   new   FileNotFoundException();  
   
  StreamReader   sr   =   f.OpenText();  
  try  
  {  
  while((line=sr.ReadLine())!=null)  
  decodedHtml   +=   Decode(line);  
   
  return   decodedHtml;  
  }  
  finally  
  {  
  sr.Close();  
  sr   =   null;  
  f   =   null;  
  }  
  }  
   
  public   static   string   Decode(string   encoded)  
  {  
  if   (encoded   ==   null)  
  throw   new   ArgumentNullException();  
   
  string   line;  
  StringWriter   sw   =   new   StringWriter();  
  StringReader   sr   =   new   StringReader(encoded);  
   
  try  
  {  
  while((line=sr.ReadLine())!=null)  
  {  
  if   (line.EndsWith("="))  
  sw.Write(HexDecoder(line.Substring(0,   line.Length-1)));  
  else  
  sw.WriteLine(HexDecoder(line));  
   
  sw.Flush();  
  }  
  return   sw.ToString();  
  }  
  finally  
  {  
  sw.Close();  
  sr.Close();  
  sw   =   null;  
  sr   =   null;  
  }  
  }  
  }  
   
  }  
  Top

5 楼purpo(漂泊的云)回复于 2005-12-01 15:39:41 得分 0

注:最近做一个项目中要用到QP编码及解码,故特意写了一个算法,以上仅供参考,希望各位多交流交流,不足之处还请赐教。Top

6 楼s5689412(华君)回复于 2005-12-01 15:59:25 得分 5

也可以参考这里:  
   
  http://www.codeproject.com/dotnet/cpSphereEmailComponent.aspTop

7 楼sugarsupper(智慧之刃)回复于 2005-12-01 16:17:14 得分 30

我找到的一个,lz看看吧  
  public   static   string   ConvertFromQPString(string   quoted_printableString)  
  {  
  string   InputString=quoted_printableString;  
  StringBuilder   builder1   =   new   StringBuilder();  
  InputString   =   InputString.Replace("=\r\n",   "");  
  for   (int   num1   =   0;   num1   <   InputString.Length;   num1++)  
  {  
  if   (InputString[num1]   ==   '=')  
  {  
  try  
  {  
  if   (HexToDec(InputString.Substring(num1   +   1,   2))   <   0x80)  
  {  
  if   (HexToDec(InputString.Substring(num1   +   1,   2))   >=   0)  
  {  
  byte[]   buffer1   =   new   byte[1]   {   (byte)   HexToDec(InputString.Substring(num1   +   1,   2))   }   ;  
  builder1.Append(Encoding.Default.GetString(buffer1));  
  num1   +=   2;  
  }  
  }  
  else   if   (InputString[num1   +   1]   !=   '=')  
  {  
  byte[]   buffer2   =   new   byte[2]   {   (byte)   HexToDec(InputString.Substring(num1   +   1,   2)),   (byte)   HexToDec(InputString.Substring(num1   +   4,   2))   }   ;  
  builder1.Append(Encoding.Default.GetString(buffer2));  
  num1   +=   5;  
  }  
  }  
  catch  
  {  
  builder1.Append(InputString.Substring(num1,   1));  
  }  
  }  
  else  
  {  
  builder1.Append(InputString.Substring(num1,   1));  
  }  
  }  
  return   builder1.ToString();  
  }  
  private   static   int   HexToDec(string   hex)  
  {  
  int   num1   =   0;  
  string   text1   =   "0123456789ABCDEF";  
  for   (int   num2   =   0;   num2   <   hex.Length;   num2++)  
  {  
  if   (text1.IndexOf(hex[num2])   ==   -1)  
  {  
  return   -1;  
  }  
  num1   =   (num1   *   0x10)   +   text1.IndexOf(hex[num2]);  
  }  
  return   num1;  
  }     //Top

相关问题

  • c算法函数求救!!1
  • 急求三角函数模拟算法!
  • 求此函数的非递归算法
  • --= 随机数函数的实现算法 =--
  • 求字符串代换函数算法
  • 急,求教quoted-printable解码函数
  • 函数与基本的算法。(取值函数)!
  • 请问哪儿能找到二叉树的算法函数?
  • 高分求购 一个算法/函数...在线等待给份
  • 谁有pb编的des算法的函数?

关键词

  • 字符
  • 编码
  • 算法
  • sour
  • qp
  • printable
  • second
  • quoted
  • bc
  • unsigned char

得分解答快速导航

  • 帖主:nik_Amis
  • dejollia
  • s5689412
  • sugarsupper

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo