2009-05-01 81 views
1

我可以說我不知道​​我在尋求什麼幫助,因爲我不知道格式,但我有一張照片。如何將byte []轉換爲該文本格式?

我有一個byte []數組,我該如何將它轉換爲下面的格式(在右邊)?

alt text http://img512.imageshack.us/img512/3548/48667724.jpg

它不是純ASCII。

+0

其放置在imageshack.us - > http://img512.imageshack.us/img512/3548/48667724.jpg – 2009-05-01 18:57:32

+0

@Ken:圖像對我來說很好 - 它看起來是十六進制編輯器的截圖。 – Noldorin 2009-05-01 18:59:12

+0

約翰,小心豬流感! – 2009-05-01 19:28:15

回答

5

使用b.ToString("x2")格式化一個字節值轉換成一個兩字符的十六進制字符串。

對於ASCII顯示,檢查值對應於一個普通可打印字符和轉換它,如果它是:

if (b >= 32 && b <= 127) { 
    c = (char)b; 
} else { 
    c = '.'; 
} 

或更短:

c = b >= 32 && b <= 127 ? (char)b : '.'; 

要做到這一點陣列上:

StringBuilder builder = new StringBuilder(); 
foreach (b in theArray) { 
    builder.Append(b >= 32 && b <= 127 ? (char)b : '.'); 
} 
string result = builder.ToString(); 
1

這可能是任何數字編碼的...嘗試這個測試的測試腳本,看看哪些人打印出:

Bl8s 

下面是腳本:

byte[] b = new byte[] {0x42, 0x6C, 0x38, 0x73 }; 
foreach (EncodingInfo ei in Encoding.GetEncodings()) 
{ 
    Console.WriteLine("{0} - {1}", ei.GetEncoding().GetString(b), ei.Name); 
} 
0

嘗試:Encoding.Default.GetBytes

如果這不起作用,您可以指定不同的類型(UTF-8,ASCII ...)

6

聽起來你想要一個字節數組,並將其轉換爲

static public string ToPrintableString(this byte[] bytes) 
{ 
    return Encoding.ASCII.GetString 
      (
       bytes.Select(x => x < 0x20 || x > 0x7E ? (byte)'.' : x) 
        .ToArray() 
      ); 
} 

(你可以稱之爲像string printable = byteArray.ToPrintableString();:文本

static public string ConvertFromBytes(byte[] input) 
{ 
    StringBuilder output = new StringBuilder(input.Length); 

    foreach (byte b in input) 
    { 
     // Printable chars are from 0x20 (space) to 0x7E (~) 
     if (b >= 0x20 && b <= 0x7E) 
     { 
      output.Append((char)b); 
     } 
     else 
     { 
      // This isn't a text char, so use a placehold char instead 
      output.Append("."); 
     } 
    } 

    return output.ToString(); 
} 

或作爲LINQy擴展方法(靜態擴展類的內部)(用「.」 S在一定範圍的外部替換字符) )

1

[編輯] 2018:]從頭開始重新編寫函數,使代碼更高效並修復一些其他問題。您現在也可以選擇指定起始偏移量和字節數(從那裏開始)來顯示。


如果希望整個存儲器顯示,包括偏置號,以及左和右顯示器,則可以做到這一點是這樣的:(32字節寬)

/// <summary> Returns a String where the specified bytes are formatted in a 
/// 3-section debugger-style aligned memory display, 32-bytes per line </summary> 
public static unsafe String MemoryDisplay(byte[] mem, int i_start = 0, int c = -1) 
{ 
    if (mem == null) 
     throw new ArgumentNullException(); 
    if (i_start < 0) 
     throw new IndexOutOfRangeException(); 
    if (c == -1) 
     c = mem.Length - i_start; 
    else if (c < 0) 
     throw new ArgumentException(); 
    if (c == 0) 
     return String.Empty; 

    char* pch = stackalloc Char[32];  // for building right side at the same time 
    var sb = new StringBuilder((c/32 + 1) * 140);   // exact pre-allocation 

    c += i_start; 
    for (int i = i_start & ~0x1F; i < c;) 
    { 
     sb.Append(i.ToString("x8")); 
     sb.Append(' '); 

     do 
     { 
      if (i < i_start || i >= c)   // non-requested area, or past the end 
      { 
       sb.Append(" "); 
       pch[i & 0x1F] = ' '; 
      } 
      else 
      { 
       var b = mem[i]; 
       sb.Append(b.ToString("x2") + " "); 
       pch[i & 0x1F] = non_monospace(b) ? '.' : (Char)b; 
      } 
     } 
     while ((++i & 0x1F) != 0); 

     sb.Append(' '); 
     sb.AppendLine(new String(pch, 0, 32)); 
    } 
    return sb.ToString(); 
} 

該代碼使用幫助者確定哪些字符在右手邊應該顯示爲'點'。上述功能的

static readonly ulong[] _nmb = 
{ 
    0x00000000ffffe7ffUL, 
    0x8000000000000000UL, 
    0x00002000ffffffffUL, 
    0x0000000000000000UL, 
}; 

static bool non_monospace(byte b) => (_nmb[b >> 6] & 1UL << b) != 0; 

輸出看起來像這樣(字符寬度爲138列,滾動到看到「人類可讀」的部分的右側):

 
00000000 47 49 46 38 39 61 0f 00 0f 00 91 ff 00 00 00 00 c0 c0 c0 ff ff 00 00 00 00 21 f9 04 01 00 00 01 GIF89a...................!...... 
00000020 00 2c 00 00 00 00 0f 00 0f 00 00 02 2c 8c 0d 99 c7 91 02 e1 62 20 5a 79 ea bd 00 6d 89 69 8a f8 .,..........,.......b Zy...m.i.. 
00000040 08 e5 a7 99 e9 17 9d ac 24 a2 21 68 89 1e ac b4 d9 db 51 ab da c8 8c 1a 05 00 3b    ........$.!h......Q.......;