C#:用于解码 Quoted-Printable 编码的类?

标签 c# string class encoding quoted-printable

C# 中是否存在可以转换Quoted-Printable 的现有类?编码为 String?单击上面的链接以获取有关编码的更多信息。

为方便起见,以下内容摘自上述链接。

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.

最佳答案

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

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!

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

关于C#:用于解码 Quoted-Printable 编码的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2226554/

相关文章:

c# - 替换 64 位机器中 dbf 文件的 Jet.OLEDB.4.0 驱动程序

c# - IPTC .NET 读/写 C# 库

c# - 添加到并发集合

c++ - C++ 中的 this->variable 和 namespace::class::variable 有什么区别?

c# - ASP.NET MVC OWIN 和 SignalR - 两个 Startup.cs 文件

Python字符串出现次数正则表达式性能

java - java 的字符串中断

sql-server - 动态 sql 和 spexecutesql 行为

java - 我怎样才能留在一个Java类中直到我准备退出

java - 我如何确定应用程序中的特定对象不再使用?