C#:类解码引用打印编码? [英] C#: Class for decoding Quoted-Printable encoding?

查看:155
本文介绍了C#:类解码引用打印编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在C#中现有的类,它可以引用打印编码转换为字符串?点击上面的链接,以获得有关编码的更多信息。

以下是从上面的链接为您提供方便引用。


  

任意8位字节的值可能是连接codeD
  有3个字符,一个=后面
  两个十六进制数字(0-9或A-F)
  再presenting字节的数值。
  例如,US-ASCII换
  字符(十进制值12)即可
  再由= 0C,和U​​S-ASCII psented $ P $
  等号(十进制值61)
  再由= 3Dpsented $ P $。所有字符
  除了可打印的ASCII字符或
  行的字符结尾必须连接codeD
  以这种方式。


  
  

所有可打印的ASCII字符
  (33和126之间的十进制值)
  可以是由他们自己psented重新$ P $,
  除了=(十进制61)。


  
  

ASCII标签和空格字符,
  十进制值9和32,可以是
  再由自己psented,除非$ P $
  这些字符出现在年底
  一条线。如果这些字符中的一个
  出现在一行它必须年底
  是连接codeD为= 09(标签)或= 20
  (空格)。


  
  

如果该数据是EN codeD包含
  有意义的换行符,它们必须是
  EN codeD为ASCII CR LF序列,
  不像他们的原始字节的值。
  相反,如果字节值13和10
  比行尾其他含义
  那么它们必须连接codeD作为= 0D和
  = 0A。


  
  

引用可打印的线条连接codeD数据
  不得超过76个字符。
  为了满足这一要求,而不
  改变EN codeD文本,软线
  根据需要可加入断裂。软
  换行包含一个=的在
  一间$ C $裁线的结束,不
  导致去codeD换行
  文字。



解决方案

有在框架库做这个功能,但它不会出现被干净地露出。执行是在内部类 System.Net.Mime.QuotedPrintableStream 。这个类定义了一个名为德codeBytes 这你想要做什么方法。该方法似乎仅由一个用来解code MIME头方法中。此方法也是内部的,但相当直接在几个地方调用,例如,在 Attachment.Name 二传手。演示:

 使用系统;
使用System.Net.Mail;命名空间的ConsoleApplication1
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            附件附件= Attachment.CreateAttachmentFromString(,= ISO-8859-1 Q = A1Hola,_se = F1或=???!?);
            Console.WriteLine(attachment.Name);
        }
    }
}

生成的输出:


  

¡霍拉,_señor!


您可能必须做一些测试,以确保回车等被视为正确虽然在快速测试我做,他们似乎是。但是,除非你用例是足够接近一个MIME头字符串,你不认为它会通过向图书馆所做的任何更改被打破的解码也未必是明智的,靠这个功能。你可能会更好编写自己的引用可打印德codeR。

Is there an existing class in C# that can convert Quoted-Printable encoding to String? Click on the above link to get more information on the encoding.

The following is quoted from the above link for your convenience.

Any 8-bit byte value may be encoded with 3 characters, an "=" followed by two hexadecimal digits (0–9 or A–F) representing the byte's numeric value. For example, a US-ASCII form feed character (decimal value 12) can be represented by "=0C", and a US-ASCII equal sign (decimal value 61) is represented by "=3D". All characters except printable ASCII characters or end of line characters must be encoded in this fashion.

All printable ASCII characters (decimal values between 33 and 126) may be represented by themselves, except "=" (decimal 61).

ASCII tab and space characters, decimal values 9 and 32, may be represented by themselves, except if these characters appear at the end of a line. If one of these characters appears at the end of a line it must be encoded as "=09" (tab) or "=20" (space).

If the data being encoded contains meaningful line breaks, they must be encoded as an ASCII CR LF sequence, not as their original byte values. Conversely if byte values 13 and 10 have meanings other than end of line then they must be encoded as =0D and =0A.

Lines of quoted-printable encoded data must not be longer than 76 characters. To satisfy this requirement without altering the encoded text, soft line breaks may be added as desired. A soft line break consists of an "=" at the end of an encoded line, and does not cause a line break in the decoded text.

解决方案

There is functionality in the framework libraries to do this, but it doesn't appear to be cleanly exposed. The implementation is in the internal class System.Net.Mime.QuotedPrintableStream. This class defines a method called DecodeBytes which does what you want. The method appears to be used by only one method which is used to decode MIME headers. This method is also internal, but is called fairly directly in a couple of places, e.g., the Attachment.Name setter. A demonstration:

using System;
using System.Net.Mail;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Attachment attachment = Attachment.CreateAttachmentFromString("", "=?iso-8859-1?Q?=A1Hola,_se=F1or!?=");
            Console.WriteLine(attachment.Name);
        }
    }
}

Produces the output:

¡Hola,_señor!

You may have to do some testing to ensure carriage returns, etc are treated correctly although in a quick test I did they seem to be. However, it may not be wise to rely on this functionality unless your use-case is close enough to decoding of a MIME header string that you don't think it will be broken by any changes made to the library. You might be better off writing your own quoted-printable decoder.

这篇关于C#:类解码引用打印编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆