C#:用于解码 Quoted-Printable 编码的类? [英] C#: Class for decoding Quoted-Printable encoding?

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

问题描述

C# 中是否存在可以将 Quoted-Printable 编码转换为 <代码>字符串?单击上面的链接以获取有关编码的更多信息.

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.

任何 8 位字节值都可以被编码有 3 个字符,一个="后跟两个十六进制数字(0–9 或 A–F)表示字节的数值.例如,US-ASCII 换页字符(十进制值 12)可以是由=0C"和一个 US-ASCII 表示等号(十进制值 61)是由=3D"表示.所有字符除了可打印的 ASCII 字符或必须对行尾字符进行编码以这种方式.

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.

所有可打印的 ASCII 字符(33 到 126 之间的十进制值)可以由他们自己代表,除了="(十进制 61).

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

ASCII 制表符和空格字符,十进制值 9 和 32,可能是由他们自己代表,除非这些字符出现在一条线.如果这些字符之一出现在一行的末尾,它必须编码为=09"(制表符)或=20"(空格).

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).

如果被编码的数据包含有意义的换行符,它们必须是编码为 ASCII CR LF 序列,不是它们的原始字节值.相反,如果字节值为 13 和 10具有除行尾以外的含义那么它们必须被编码为 =0D 和=0A.

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.

多行带引号的可打印编码数据不得超过 76 个字符.为了满足这个要求而无需改变编码文本,软线可以根据需要添加休息时间.一个软换行符由="组成编码行的结尾,并且不导致解码中的换行符文本.

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.

推荐答案

框架库中有一些功能可以做到这一点,但它似乎没有完全公开.实现在内部类 System.Net.Mime.QuotedPrintableStream 中.此类定义了一个名为 DecodeBytes 的方法,它可以执行您想要的操作.该方法似乎仅由一种用于解码 MIME 标头的方法使用.这个方法也是内部的,但在几个地方被直接调用,例如,Attachment.Name setter.演示:

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);
        }
    }
}

产生输出:

¡Hola,_señor!

¡Hola,_señor!

您可能需要进行一些测试以确保正确处理回车等,尽管在快速测试中我似乎确实如此.但是,依赖此功能可能并不明智,除非您的用例足够接近解码 MIME 标头字符串,并且您认为对库所做的任何更改都不会破坏它.您最好编写自己的带引号的可打印解码器.

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#:用于解码 Quoted-Printable 编码的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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