c# - 转换问题

标签 c#

有人可以告诉我如何在 C# 中执行此操作吗?

从 Quoted Printable 转换为二进制,然后使用 UTF-8 编码将该二进制解码为文本。

这是我需要引用的文本示例:

“评论:Je suis abonn=C3=A9 =C3=A0 la livraison =C3=A0 domicile depuis longtemps。”

最佳答案

好的,所以你的问题基本上是两个问题合二为一。首先,您需要能够解码quoted-printable。我假设您将编码文本作为字符串。您必须使用 while 循环遍历该字符串,如下所示。我故意省略了将两个十六进制字符转换为字节的部分;我相信您可以自己解决这个问题:)

var i = 0;
var output = new List<byte>();
while (i < input.Length)
{
    if (input[i] == '=' && input[i+1] == '\r' && input[i+2] == '\n')
    {
        // skip this
        i += 3;
    }
    else if (input[i] == '=')
    {
        byte b = (construct the byte from the characters input[i+1] and input[i+2]);
        output.Add(b);
        i += 3;
    }
    else
    {
        output.Add((byte)input[i]);
        i++;
    }
}

最后,输出包含原始字节。现在您需要做的就是使用 UTF8 对其进行解码:

var outputString = Encoding.UTF8.GetString(output.ToArray());

如有任何疑问,请在评论中提问。请记住:不要复制和使用您不理解的代码:)

关于c# - 转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3388934/

相关文章:

c# - ASP .NET 中 DataBind() 函数的反转是什么

c# - 如何从带有 MethodCallExpression/lambda 的树中的 ConditionalExpression.IfThen 返回?

c# - Appfabric 缓存大小

c# - 简单的 Windows 窗体数据绑定(bind)

c# - Azure 日志分析 .NET SDK 中的跨工作区查询

c# - 为什么 roslyn 对于未签名的引用给出警告 CS8002 而不是错误 CS1577?

c# - Newtonsoft Json 反序列化器不释放内存

c# - 解析 XAML : "Integer types not allowed" errors for many attributes such as width 时出错

c# - C# 中的数组索引越界异常

c# - 在 LinQ 中检查 null