【问题标题】:Convert ASCII file to Decimal file将 ASCII 文件转换为十进制文件
【发布时间】:2015-03-03 14:10:22
【问题描述】:

您好,我有一个带有(ASCII 或其他格式,我不知道可能是 UTF 或 UNICODE)文本的文本文件。在这个文件中是特殊字母。我想将所有字母,完整文件转换为 DECIMAL。我只是不知道怎么做。

private void simpleButton1_Click(object sender, EventArgs e)
{
    int size = -1;
    DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
    if (result == DialogResult.OK) // Test result.
    {
        string file = openFileDialog1.FileName;
        try
        {
            string text = File.ReadAllText(file);
            //call a function that converts the string text into decimal
            size = text.Length;
            memoEdit1.Text = text;
        }
        catch (IOException)
        {
        }
    }
    Console.WriteLine(size); // <-- Shows file size in debugging mode.
    Console.WriteLine(result); // <-- For debugging use.
}

这是到目前为止的代码......

更新:

文件看起来像这样(ASCII 或其他我不知道的格式,可能是 UTF 或 UNICODE)。这只是示例(ASCII 或其他我不知道可能是 UTF 或 UNICODE 的格式)代码。

ae 欧欧 ü § 磷 ♀ !呸呸

在转换后它应该只是十进制数字。

再举一个例子 文件看起来像这样(ASCII):äüö 转换后它应该看起来像这样(DECIMAL):228 252 246

【问题讨论】:

  • 对不起...你把它转换成十进制到底是什么意思?
  • 确实如此。请添加一些示例输入和输出——您想要实现的示例。
  • 文件看起来像这样(ASCII):äüö 转换后它应该看起来像这样(DECIMAL):228 252 246
  • 请用简短版本的样本大小(所以不是 1000 行文本)和所需的输出更新您的问题,这样我们就不必在 cmets 中搜索它
  • 在我看来,您的文件可能是二进制文件而不是 ascii。如果是这样,第一个问题将阅读 ascii

标签: c# file decimal ascii


【解决方案1】:

得到该结果的演示:

class Program
{
    static void Main(string[] args)
    {
        string unconverted = "äüö"; // is what you get when using File.ReadAllText(file)

        byte[] converted = Encoding.Unicode.GetBytes(unconverted);

        converted = converted.Where(x => x != 0).ToArray(); //skip bytes that are 0

        foreach (var item in converted)
        {
            Console.WriteLine(item);
        }

    }
}

【讨论】:

  • 感谢您的快速回答我正试图让您的代码在我的项目中运行
【解决方案2】:

现在感谢它按我的意愿工作。

namespace WindowsFormsApplication2

{ 公共部分类Form1:表格 { 公共表格1() { 初始化组件(); }

    private void simpleButton1_Click(object sender, EventArgs e)
    {
        int size = -1;
        DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
        if (result == DialogResult.OK) // Test result.
        {
            string file = openFileDialog1.FileName;
            try
            {
                string text = File.ReadAllText(file);
                size = text.Length;

                //string unconverted = "äüö"; // is what you get when using File.ReadAllText(file)
                string unconverted = text;
                byte[] converted = Encoding.Unicode.GetBytes(unconverted);

                converted = converted.Where(x => x != 0).ToArray(); //skip bytes that are 0

                foreach (var item in converted)
                {
                    Console.WriteLine(item);
                    memoEdit1.Text =  memoEdit1.Text + item.ToString();
                    memoEdit1.Text = memoEdit1.Text + " "; //just for the Look and Feel :)
                }

               // memoEdit1.Text = text;
            }
            catch (IOException)
            {
            }
        }
        Console.WriteLine(size); // <-- Shows file size in debugging mode.
        Console.WriteLine(result); // <-- For debugging use.
    }

}

}

【讨论】:

  • 如果不是您自己的答案,请不要写...而是接受答案...其他用户会看到问题已解决
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-20
  • 2020-06-28
  • 1970-01-01
  • 2014-11-11
  • 2016-08-31
  • 1970-01-01
相关资源
最近更新 更多