2009-05-01 5 views
1

私は形式を知らないので、助けを求めているものがわからないと言うことができますが、私は写真を持っています。byte []をそのテキスト形式に変換するには?

私はbyte []配列を持っていますが、それをどのように(右の)そのフォーマットに変換すればよいですか?

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

そのないプレーンなASCII。

+0

その - > http://img512.imageshack.us/img512/3548/48667724.jpg –

+0

@Ken:画像は私にとってうまく表示されます。これは、16進エディタのスクリーンショットのようです。 – Noldorin

+0

ジョン、豚インフルエンザに注意してください! –

答えて

5

使用b.ToString("x2")形式にバイト値を2文字の16進文字列に変換します。 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 ...)があります。

+0

Encoding.DefaultはANSIコードページを表していますが、それは明らかにそうではありません(複数バイトで開始)。 – Noldorin

6

バイト配列を使用して、 (「.」sの一定の範囲外の文字を置き換える)テキスト

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の拡張メソッドとして:

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();のようなものを呼び出すことができます)

+0

20の代わりに0x20を意味すると思います。または、私が投稿したコードのように32を使用してください。 – Guffa

+0

この関数は、ASCIIまたはANSIエンコーディングでのみ機能します。 – Noldorin

+0

ええ、私はそれをキャッチした;ありがとう; - ] –

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列で、「人間が読める」の部分を参照するには右にスクロール)のようになります。imageshack.usに配置

 
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.......; 
関連する問題