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

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

问题描述

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

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表单Feed
字符(十进制值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,可以是
表示的自己,除非
这些字符出现在
a行的结尾。如果这些字符之一
出现在行的末尾,它必须将
编码为= 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个字符。
为了满足这个要求,没有
改变编码的文本,可以根据需要添加软线
break。软
换行符在编码行的
结尾处由=组成,而不是
在解码的
文本中产生换行符。

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

产生输出: p>

Produces the output:


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

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