【问题标题】:Problem Read file with US-Ascii Encoding in Dotnet 5? (Work on Dotnet Fx 4.6.2)问题在 Dotnet 5 中使用 US-Ascii 编码读取文件? (在 Dotnet Fx 4.6.2 上工作)
【发布时间】:2021-03-24 03:43:50
【问题描述】:

我有这样的代码:

int fLen = FileSystem.FreeFile();
        FileSystem.FileOpen(fLen, txtFilename.Text, OpenMode.Binary, OpenAccess.Read, OpenShare.Shared, -1);

        tmp = new String(' ', Convert.ToInt32(FileSystem.FileLen(txtFilename.Text)));

        FileSystem.FileGet(fLen, ref tmp, -1, false);
        FileSystem.FileClose(fLen);

我有平台 dotnet framework 4.6.2, ,变量 tmp 有结果:

xƒ{‚{{{{{\u007f{\u007f{~{|x‡x„zzzzzzzzzzzzzz{€{zxƒ{|{z{{{\u007fz‡{z{ƒz‡{z{‚zz{{{\u0081{„{{{}{„{}{|xƒ{{xƒ{|xƒ{{xƒ{zx‡x

当我将我的 winforms 升级到 dotnet 5 时,变量 tmp 有结果:

x�{�{{{{{\u007f{\u007f{~{|x�x�zzzzzzzzzzzzzz{�{zx�{|{z{{{\u007fz�{z{�z�{z{�zz{{{�{�{{{}{�{}{|x�{{x�{|x�{{x�{zx�x�zzzzzzzzzzzzzzzz{{x�{|{z{{{\u007fz

dotnet 5 有什么问题?当我读取文件文本时它会改变格式吗?

原始文本文件是:(我从记事本复制粘贴)

xƒ{‚{{{{{{{~{|x‡x„zzzzzzzzzzzzzz{€{zxƒ{|{z{{{z‡{z{ƒz‡{z{‚zz{{{{„{{{}{„{}{|xƒ{{xƒ{|xƒ{{xƒ

我用的是 binaryreader,也不好用。

            var eee = Encoding.UTF7;            
        StringBuilder sb = new StringBuilder();
        FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
        //creating BinaryReader using Stream object
        using BinaryReader reader = new BinaryReader(stream, eee);
        var tes = eee.GetString(eee.GetBytes(reader.ReadString()));

编码为:“US-ASCII”

任何帮助将不胜感激。

更新:

来自示例 BioBridgeSDK 的完整代码:

private void btnDecryptAttLog_Click(object sender, EventArgs e)
    {
        string val1 = "";
        string val2 = "";
        string tmp = "";
        Form4 myForm4 = new Form4();

        int fLen = FileSystem.FreeFile();
        FileSystem.FileOpen(fLen, txtFilename.Text, OpenMode.Binary, OpenAccess.Read, OpenShare.Shared, -1);

        tmp = new String(' ', Convert.ToInt32(FileSystem.FileLen(txtFilename.Text)));

        FileSystem.FileGet(fLen, ref tmp, -1, false);
        FileSystem.FileClose(fLen);

        val1 = tmp;
        string ReadFile = val1;

        int iRead = 10000;
        int iPos = 0;
        String FullData = "";
        String SubData = "";

        while (val1.Length > iPos)
        {
            if ((iPos + iRead) > val1.Length)
                iRead = val1.Length - iPos;

            val2 = "";
            SubData = val1.Substring(iPos, iRead);

            if (axBioBridgeSDKv3lib1.DecryptLog(SubData, ref val2) == 0)
                FullData = FullData + val2;

            iPos = iPos + iRead;
        }

        myForm4.Text1.Text = FullData;
        myForm4.Show();
    }

【问题讨论】:

  • 这是什么东西:FileSystem.FreeFile() 等等?这看起来像是来自 VB.Net 或 VB6 的一个奇怪的端口,它使用了古老的方法。使用 .Net 方法保存您的数据。 UTF-8 是默认编码。 US-ASCII 不存在。 Encoding.ASCII 确实如此。但你需要Encoding.UTF8
  • 文件的编码不能是ASCII,因为ƒ 不是和ASCII字符。
  • FileSystem.FreeFile() 来自 Microsoft.VisualBasic 命名空间(我在 dotnet 5 上使用 nuget),我知道它来自 VB6,问题是,我使用“Fingertec”机器。我从机器解密日志文件(考勤日志),BioBridge SDK 的解决方法就是这样。 (也许使用旧代码)。我已经通过电子邮件发送至fingertec 支持,而不是他们的回复。我只想从考勤日志文件中读取,而不是写入文件。谢谢。交替尝试 UTF8、UTF7、UTF32、Unicode。没有任何效果。
  • 你能显示字节数组(该文本文件的值读取为二进制)吗?一行就足够了(从文件的最开始)。顺便说一句,如果记事本可以正确读取文件,也许它只是使用Encoding.Default(您的本地代码页)。
  • 如何显示文本文件的字节数组?我的记事本可以正确读取文件。不改变一切。

标签: c# winforms encoding ascii


【解决方案1】:

尝试传递编码。

例如

var text = File.ReadAllText("path", Encoding.ASCII);

或者对于大文件使用StreamReader。不要使用BinaryReader阅读文本文件

using (var reader = new StreamReader("path", Encoding.ASCII))
{
    string line = "";

    while ((line = reader.ReadLine()) != null)
    {
        //Do something with the line.
    }
}

另外,在您的二进制阅读器代码中,您为什么将编码设置为 UTF-7 ? var eee = Encoding.UTF7;

【讨论】:

  • 不起作用。我设置为 utf-7,也许 us-ascii 是 7 位的?基于“charset.org/charsets/us-ascii”,我使用来自“fingertec.com/developerprogram/sdk.php#”的样本
  • 结果还有字符“?”
  • @ChandraArifin ...在他们关于文件编码的问题中误导了他们。没有ƒ 的文件可以是 ASCII 文本。 UTF8 更有可能......所以没有理由建议无法处理问题中的文件的编码。理想情况下,他们会用十六进制值显示一小部分......而不是文本......
  • @ChandraArifin 尝试使用 Notepad++ 之类的文本编辑器打开文件并查看它检测到的编码
  • 使用notepad++打开时,编码为ANSI
【解决方案2】:

我已经找到解决方案了。

var text = File.ReadAllBytes(file);
var text1 = Encoding.GetEncoding(1252).GetString(text);

感谢您的评论,我通过阅读评论找到了解决方案。 @jimi 和其他cmets。谢谢。

【讨论】:

  • 为了完整起见:编码 1252 是 Windows-1252 /西欧,而不是 ASCII。
猜你喜欢
  • 2021-06-06
  • 2016-02-08
  • 2018-02-27
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-20
相关资源
最近更新 更多