2012-09-11 45 views
1

是的,我已經閱讀了大約5個小時的解決方案,但都沒有工作。 BitConverter只是創建一個空白字符串。將ByteArray轉換爲用於TextBox c中的字符串#

基本上我正在做的是試圖創建一個級別閱讀器,它將通過十六進制讀取一個級別的內容,並最終顯示在樹形視圖中。所以我必須做的第一件事是創建一個可以編輯數據的字節數組,我已經完成了。 但是,現在我想要在屏幕上顯示數據。據我所知,你不能在屏幕上顯示一個字節數組,你必須先將它轉換爲一個字符串。

所以,這就是我想要做的事:

 using (OpenFileDialog fileDialog = new OpenFileDialog()) 
     { 
      if (fileDialog.ShowDialog() != DialogResult.Cancel) 
      { 
       textBox1.Text = fileDialog.FileName; 
       using (BinaryReader fileBytes = new BinaryReader(new MemoryStream(File.ReadAllBytes(textBox1.Text)))) 
       { 
        string s = null; 
        int length = (int)fileBytes.BaseStream.Length; 
        byte[] hex = fileBytes.ReadBytes(length); 
        File.WriteAllBytes(@"c:\temp_file.txt", hex); 
       } 
      } 
     } 
    } 

注:我已刪除了我的轉換嘗試如無我試圖努力。 有誰知道我可以如何使用這些數據並將其轉換爲字符串,並將其添加到文本框? (當然,我知道怎麼做後者,前者我有困難。) 如果是這樣,請提供例子。

編輯:

我也許應該讓自己更清楚;我不想將字節轉換爲相應的字符(即如果它是0x43,我不想打印'C'。我想打印'43'。

PS我試過一些在答案和大多數工作中的答案,但太慢,不能使用。其他答案將是美好的:)

+0

我懷疑你不知道 - 但什麼文字編碼呢你二進制文件格式使用? – tomfanning

+0

問題是,我們不知道您嘗試過哪些解決方案。即你見過[這個答案](http://stackoverflow.com/a/311179/15498)?另外,在二進制閱讀器的內存流中封裝一個字節數組,然後將其轉換回字節數組? –

+0

@Damien_The_Unbeliever - 接受答案中的兩種方法實際上都有效;但它們只能用於大約10kb的文件。其他任何事情都需要很長時間才能實現。 (超過1分鐘閱讀)。 任何想法? – Anteara

回答

2

你知道在你的編碼字節數組存儲?

你需要Encoding.GetString方法

這裏是MSDN例如

using System; 
using System.IO; 
using System.Text; 

public class Example 
{ 
    const int MAX_BUFFER_SIZE = 2048; 
    static Encoding enc8 = Encoding.UTF8; 

    public static void Main() 
    { 
     FileStream fStream = new FileStream(@".\Utf8Example.txt", FileMode.Open); 
     string contents = null; 

     // If file size is small, read in a single operation. 
     if (fStream.Length <= MAX_BUFFER_SIZE) { 
     Byte[] bytes = new Byte[fStream.Length]; 
     fStream.Read(bytes, 0, bytes.Length); 
     contents = enc8.GetString(bytes); 
     } 
     // If file size exceeds buffer size, perform multiple reads. 
     else { 
     contents = ReadFromBuffer(fStream); 
     } 
     fStream.Close(); 
     Console.WriteLine(contents); 
    } 

    private static string ReadFromBuffer(FileStream fStream) 
    { 
     Byte[] bytes = new Byte[MAX_BUFFER_SIZE]; 
     string output = String.Empty; 
     Decoder decoder8 = enc8.GetDecoder(); 

     while (fStream.Position < fStream.Length) { 
      int nBytes = fStream.Read(bytes, 0, bytes.Length); 
      int nChars = decoder8.GetCharCount(bytes, 0, nBytes); 
      char[] chars = new char[nChars]; 
      nChars = decoder8.GetChars(bytes, 0, nBytes, chars, 0); 
      output += new String(chars, 0, nChars);              
     } 
     return output; 
    } 
} 
// The example displays the following output: 
//  This is a UTF-8-encoded file that contains primarily Latin text, although it 
//  does list the first twelve letters of the Russian (Cyrillic) alphabet: 
//  
//  А б в г д е ё ж з и й к 
//  
//  The goal is to save this file, then open and decode it as a binary stream. 

編輯

如果你想在十六進制格式打印出來的字節數組,BitConverter是你在找什麼形式,這裏是MSDN示例

// Example of the BitConverter.ToString(byte[ ]) method. 
using System; 

class BytesToStringDemo 
{ 
    // Display a byte array with a name. 
    public static void WriteByteArray(byte[ ] bytes, string name) 
    { 
     const string underLine = "--------------------------------"; 

     Console.WriteLine(name); 
     Console.WriteLine(underLine.Substring(0, 
      Math.Min(name.Length, underLine.Length))); 
     Console.WriteLine(BitConverter.ToString(bytes)); 
     Console.WriteLine(); 
    } 

    public static void Main() 
    { 
     byte[ ] arrayOne = { 
      0, 1, 2, 4, 8, 16, 32, 64, 128, 255 }; 

     byte[ ] arrayTwo = { 
      32, 0, 0, 42, 0, 65, 0, 125, 0, 197, 
      0, 168, 3, 41, 4, 172, 32 }; 

     byte[ ] arrayThree = { 
      15, 0, 0, 128, 16, 39, 240, 216, 241, 255, 
      127 }; 

     byte[ ] arrayFour = { 
      15, 0, 0, 0, 0, 16, 0, 255, 3, 0, 
      0, 202, 154, 59, 255, 255, 255, 255, 127 }; 

     Console.WriteLine("This example of the " + 
      "BitConverter.ToString(byte[ ]) \n" + 
      "method generates the following output.\n"); 

     WriteByteArray(arrayOne, "arrayOne"); 
     WriteByteArray(arrayTwo, "arrayTwo"); 
     WriteByteArray(arrayThree, "arrayThree"); 
     WriteByteArray(arrayFour, "arrayFour"); 
    } 
} 

/* 
This example of the BitConverter.ToString(byte[ ]) 
method generates the following output. 

arrayOne 
-------- 
00-01-02-04-08-10-20-40-80-FF 

arrayTwo 
-------- 
20-00-00-2A-00-41-00-7D-00-C5-00-A8-03-29-04-AC-20 

arrayThree 
---------- 
0F-00-00-80-10-27-F0-D8-F1-FF-7F 

arrayFour 
--------- 
0F-00-00-00-00-10-00-FF-03-00-00-CA-9A-3B-FF-FF-FF-FF-7F 
*/ 
+0

+1。如果源文本是有用的(也似乎不是這個問題)。 –

+0

@AlexeiLevenkov,如果我們想將字節數組轉換爲文本,它應該包含任何編碼中的文本字節,我錯了嗎? –

+0

我不知道OP想要什麼「將字節數組轉換爲文本」......但聽起來OP希望至少有可打印輸出(可能至少在0-31範圍外的字符)。您的示例在大多數情況下不會提供可打印的輸出,不確定您的意思是「任何編碼」:如果源是文本並且您選擇了錯誤的編碼 - 將得到不可用的輸出(或者對於無效的UTF8序列是異常的)字節序列不能用任何正常的編碼轉換爲可打印的文本... –

1

首先,你只需要將你的字節轉換爲更有用的東西,例如UTF8,然後你可以從中獲取字符串。 喜歡的東西(在我的情況:ISO-8859-1):

buf = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, buf); 
tempString = Encoding.UTF8.GetString(buf, 0, count); 
+0

那真是令人費解! – tomfanning

+0

如果源數據不是字符串,在這之後它不會變得更好......並且也可能因爲並非每個字節序列都是有效的UTF8序列而簡單地拋出。 –

2

您可以將數據轉換爲十六進制:

StringBuilder hex = new StringBuilder(theArray.Length * 2); 
    foreach (byte b in theArray) 
    hex.AppendFormat("{0:x2}", b); 
    return hex.ToString(); 
+0

+1 。這會讓你十六進制......通常用LINQ的單一語句完成:)但是這對非專家來說更具可讀性。 –

+0

我試圖做到這一點,並在打印時,它打印的所有內容都是'System.Byte []'。 這就是我所做的: http://pastebin.com/XTJW2iAS 閱讀:'長度'是在我原來的帖子中定義的。 我可能做錯了什麼... – Anteara

+0

很簡單... textBox2.Text = hex.ToString(); 應該是 textBox2.Text = hex1.ToString(); hex1而不是hex ... – Eden

1

實在是沒有默認的方式來顯示隨機字節序列。選項:

  • 的base64編碼(Convert.ToBase64String) - 會產生不可讀的,但是至少安全地打印的字符串
  • HEX編碼每個字節,並與每個字節的表示之間的空間並置,潛在地分成幾組(即16個字節)。 - 會產生更多黑客般的觀點。
  • 地圖打印字符(潛在彩色),並可能在多行分割的所有字節 - 將產生的數據矩陣狀的圖...
相關問題