.net - 是否有一种 .NET 编码类型可以将基础文件中的每个字节作为具有相同序数值的字符返回?

标签 .net character-encoding binary

这是我的问题:

Is there a .NET Encoding object/type that will decode every byte in a file to a character with the exact same ordinal value as the one in the file, basically do a 1-to-2 mapping between a byte in the file an the character ordinal value?

更多详细信息

我正在读取文本数据,其中包含一些二进制值,即。编码为 4 个字节的整数。数据必须通过 TextReader 类读取,因为我是从外部程序标准输出获取数据。由于编码问题,我返回的数据有时会被破坏。基本上,.NET 流正在解码来自外部程序的数据,有时会切换出一个字符,以便外部程序输出的任何字节/字符序数值都与我在 .NET 中读取的值不同。

背景信息

我正在通过标准输入/输出与外部程序 Mercurial 进行通信,由于某种原因,他们决定将一些数据输出为二进制。

协议(protocol)如下所示:

<type:single-byte char><length:32-bit integer><data:string>

类型是一个单字节字符,它只是告诉我这是错误输出、标准输出还是执行命令的结果。

长度是一个 32 位整数,在流上以 4 个字节输出。

数据是一个字符串,由上述长度的字节序列组成,但这些字符可以使用 Mercurial 的默认编码进行编码。

例如,如果我要求 Mercurial 使用代码页 1252(标准 Windows)编码,则字符串将以该编码进行编码。

但是,问题是:长度当然不会

如果我将 .NET Process 对象配置为使用 Windows-1252 作为 StandardOutput 流的编码,如下所示:

psi.StandardOutputEncoding = Encoding.GetEncoding("Windows-1252");
psi.StandardErrorEncoding = Encoding.GetEncoding("Windows-1252");

然后在某个时刻,来自客户端的数据解码不同步,因为其中一个二进制长度值最终被解码,因此具有与文件中的字节不同的序数值。

我当前的示例在某个时刻包含欧元字符(作为可打印字符),但是文件中的字节没有值 172,而该值是可打印字符的值。一些解码已经发生。

但是,假设我有一个包含所有可能的字节值的文件。

然后,我通过 TextReader 后代之一打开文件,并指定编码。

是否有任何编码可以让我使用 TextReader.Read()方法并从该文件中读取每个字节,保持不变?

基本上,我的解码循环如下所示:

read one byte, convert to character
if character is 'r', 'e' or 'o':
    read next 4 bytes, assemble to integer
    read next X bytes (x=integer above)
    decode the bytes to a string using the encoding specified

但是,我尝试了此操作,当长度包含欧元字符(作为可打印字符)时,它失败了。显然,该字符在文件中具有一个字节值,但被解码为另一个字节值。

总结一下:

Is there a .NET Encoding object/type that will decode every byte in a file to a character with the exact same ordinal value as the one in the file, basically "no encoding"?

最佳答案

使用的正确编码是“iso-8859-1”,它将每个字节解码为相同的字符序数。显然,它也是 .NET(至少在我的机器上)中存在的唯一具有这种能力/特性的编码。

我写了一个LINQPad测试程序来解决这个问题:

void Main()
{
    byte[] buffer = new byte[256];
    for (int index = 0; index < 256; index++)
        buffer[index] = (byte)index;

    foreach (var encodingInfo in Encoding.GetEncodings())
    {
        string s = encodingInfo.GetEncoding().GetString(buffer);
        var stream = new MemoryStream(buffer);
        var reader = new StreamReader(stream, encodingInfo.GetEncoding());
        bool equal = true;
        for (int index = 0; index < 256; index++)
            if (reader.Read() != index)
            {
                equal = false;
                break;
            }
        if (equal)
            Debug.WriteLine(encodingInfo.Name);
    }
}

关于.net - 是否有一种 .NET 编码类型可以将基础文件中的每个字节作为具有相同序数值的字符返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6644060/

相关文章:

c# - 如果元素来自 ItemSsource,如何在 WPF TreeView 中聚焦元素?

.net - Microsoft Azure 云存储上的 Cors 错误

c++ - 将字符转换为 UTF-8 背后的逻辑

python - 不同数量的 XOR

c# - 登录C#网站时出错

c# - 该接口(interface)是否已存在于标准 .NET 库中?

java - Jsoup - 使用字符集 iso-8859-1 解析 HTML 文件

java - Android 数据绑定(bind)生成非 UTF-8 编码的文件

serialization - Erlang 二进制协议(protocol)序列化

python - 在python中将二进制缓冲区写入文件