2015-03-03 101 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

相關問題