gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:18:12 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com