2015-03-03 100 views
0

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

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

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

UPDATE:

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

AEä OEö UEü § P ♀ ! uæõ

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

另一个例子 文件看起来像这样(ASCII):228 252 246

+4

对不起......究竟你将其转换为十进制是什么意思? – Ceisc 2015-03-03 14:12:34

+1

确实。请添加一些示例输入和输出 - 您想要实现的示例。 – piojo 2015-03-03 14:14:03

+0

该文件看起来像这样(ASCII):äüö和转换后应该看起来像这样(DECIMAL):228 252 246 – 2015-03-03 14:17:04

回答

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

    } 
} 
+0

感谢您的快速答案我试图让您的代码在我的项目中工作 – 2015-03-03 14:32:21

+0

File.ReadAllText ()有一个超载,也需要一个编码... – 2015-03-03 14:36:34

+0

内部c#使用unicode虽然 – 2015-03-03 14:37:19

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

} 

}

+0

不要写答案,如果它不是你自己的...而是接受答案...其他用户会看到问题是解决 – 2015-03-03 14:41:11

+0

谢谢在Stackoverflow新的 – 2015-03-04 13:54:00

相关问题