将ASCII文件转换为Decimal文件

时间:2015-03-03 14:10:22

标签: c# file decimal ascii

您好我有一个文本文件(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ä oeö ü § P ♀ ! uæõ

并且在转换之后它应该只是DECIMAL数字。

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

2 个答案:

答案 0 :(得分: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);
        }

    }
}

答案 1 :(得分:0)

现在感谢我的工作,因为我想要它。

namespace WindowsFormsApplication2

{     公共部分类Form1:表格     {         公共Form1()         {             的InitializeComponent();         }

    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.
    }

}

}

相关问题