質問

形式を知らないので、助けを求めているものがわからない、と言うことができますが、写真があります。

byte []配列がありますが、以下の形式(右)に変換するにはどうすればよいですか?

代替テキストhttp://img512.imageshack.us/img512/3548/48667724 .jpg

単なるアスキーではありません。

役に立ちましたか?

解決

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

他のヒント

バイトの配列を取得してテキストに変換したいようです(特定の範囲外の文字を&quot; &quot; 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(); のように呼び出すことができます)

これは、任意の数のエンコーディングである可能性があります...このテストスクリプトを試して、どれが印刷されるかを確認してください:

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

[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.......;

試してください:Encoding.Default.GetBytes

それでも機能しない場合は、指定できるさまざまなタイプがあります(UTF-8、ASCII ...)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top