如何阅读Unicode字符?像一个”

public static string Read(int length, string absolutePath)
{
    StringBuilder resultAsString = new StringBuilder();

    using (MemoryMappedFile memoryMappedFile = MemoryMappedFile.CreateFromFile(absolutePath))
    using (MemoryMappedViewStream memoryMappedViewStream = memoryMappedFile.CreateViewStream(0, length))
    {
        for (int i = 0; i < length; i++)
        {
            int result = memoryMappedViewStream.ReadByte();

            if (result == -1)
            {
                break;
            }
            char letter = (char)result;

            resultAsString.Append(letter);
        }
    }

    return resultAsString.ToString();
}


读取的int(结果)为195,而char强制类型转换未提供预期的结果。

最佳答案

不确定是不是您要的,但是可以使用StreamReader

StreamReader sr = new StreamReader(stream, Encoding.Unicode);

09-25 17:10