将 ASCII 文件转换为十进制文件

Convert ASCII file to Decimal file

您好,我有一个文本文件(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 ä 哦哦 对你 § P ♀ ! uæõ

转换后只能是十进制数。

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

获得该结果的演示:

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);
        }

    }
}

感谢它现在按我想要的方式工作。

namespace WindowsFormsApplication2

{ public 部分 class Form1 : 表格 { public 表格 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.
    }

}

}