0

バイト配列を出力にトレースするためにしばらく使用していた古いヘルパー メソッドを見ています。私はずっと前にそれを書きましたが、うまく機能していますが、それを行うためのより良い方法があるかどうか疑問に思っていました (より少ないコードで)。Linq が思い浮かびましたが、私が持っている解決策は非常に非効率的です。私が必要とするのは、「foreach16」の行に沿ったもの、または一度に1つの要素を返す代わりに、列挙可能な要素のグループを返す列挙子です。私自身の列挙子クラスを作成する以外に、それを行う組み込みの方法はありますか?

以下の例には、私が達成しようとしていることに関する詳細情報があります。

元のコード

    static void PrintBytes(byte[] bytes)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.Length; i++)
        {
            if (i > 0 && ((i % 16) == 0))
            {
                // end of line, flushes bytes and resets buffer
                Console.WriteLine("   {0}", sb.ToString());
                sb.Length = 0;
            }
            else if (i > 0 && ((i % 8) == 0))
            {
                Console.Write(" ");
                sb.Append(' ');
            }

            Console.Write(" {0:X2}", (int)bytes[i]);
            if (' ' <= bytes[i] && bytes[i] <= '~')
            {
                sb.Append((char)bytes[i]);
            }
            else
            {
                // non-ASCII or control chars are printed as '.'
                sb.Append('.');
            }
        }

        // flushes the last few bytes
        if ((bytes.Length % 16) > 0)
        {
            // prints spaces where the missing bytes would be
            int spacesToPrint = 3 * (16 - (bytes.Length % 16));
            if ((bytes.Length % 16) <= 8)
            {
                spacesToPrint++;
            }

            Console.Write(new string(' ', spacesToPrint));
        }

        Console.WriteLine("   {0}", sb.ToString());
    }

私が今持っているもの - これは私がコードを単純化しようとしたものです。しかし、私は多くの Skip/Take を実行しているため、コードの複雑さが線形から二次に増加しています。

    static void PrintBytesV2(byte[] bytes)
    {
        for (int i = 0; i < bytes.Length; i += 16)
        {
            PrintLineV2(bytes, i, Math.Min(16, bytes.Length - i));
        }
    }

    static void PrintLineV2(byte[] array, int offset, int count)
    {
        Console.Write(
            string.Join(
                " ", 
                array
                    .Skip(offset)
                    .Take(count)
                    .Select((b, i) =>
                        ((i == 8) ? " " : "") +
                            string.Format("{0:X2}", (int)b))));

        Console.Write( 
            new string(
                ' ', 
                (16 - count) * 3 +
                    (count <= 8 ? 1 : 0)) + 
            "  ");

        Console.WriteLine(
            string.Join(
            "", 
            array
                .Skip(offset)
                .Take(count)
                .Select(b => (' ' <= b && b <= '~') ? (char)b : '.')));
    }

新しいコードがそのまま線形であったとしても、1) 動作するので元のコードに固執する可能性が高いことに注意してください。2) より読みやすいと思います。しかし、グループを反復処理する方法があるかどうか疑問に思わずにはいられません。

4

4 に答える 4

1

foreach16() 型の実装なら、これはどうでしょうか。

var sampleSet = Enumerable.Range(0, 200);
sampleSet.ForEachBlock(16, x => Console.WriteLine(string.Join(",", x)));

...

この拡張メソッドを使用する:

public static void ForEachBlock<T>(this IEnumerable<T> source, int blockSize, Action<IEnumerable<T>> action)
{
    foreach (var group in source.Select((x, index) => new { x, index }).GroupBy(x => x.index / blockSize, y => y.x))
    action(group);
}
于 2012-05-21T04:46:38.417 に答える
1

必要なものは次のとおりです。

var result =
    String.Join("\n",
        bytes
            .Select((b, i) => new { b, i })
            .GroupBy(x => x.i / 16, x => x.b)
            .Select(bs =>
                String.Join(" ",
                    bs.Select(b =>
                        String
                            .Format("{0:X2}", b)))
                            .PadRight(16 * 3, ' ')));

上記のコードを「ザ・クイック・ブラウン・フォックス」でテストしました。(UTF8 を使用) すると、次の出力が得られます。

54 68 65 20 71 75 69 63 6B 20 62 72 6F 77 6E 20 
66 6F 78 2E                                     

私は明らかに最初のバージョンで少し性急でした。これでもう少し完成度が上がるかもしれません。

Func<string, IEnumerable<byte>> toBytes =
    x => System.Text.UTF8Encoding.UTF8.GetBytes(x);

Func<IEnumerable<byte>, string> toString =
    x => System.Text.UTF8Encoding.UTF8.GetString(x.ToArray());

Func<IEnumerable<byte>, string> toHexBlock =
    xs => String.Join(" ", xs.Select(x => String.Format("{0:X2}", x)));

Func<IEnumerable<byte>, string> toHexLine =
    xs =>
        String
            .Format("{0}  {1}",
                toHexBlock(xs.Take(8)),
                toHexBlock(xs.Skip(8).Take(8)))
            .PadRight(16 * 3 + 1, ' ')
        + String
            .Format("{0} {1}",
                toString(xs.Take(8)),
                toString(xs.Skip(8).Take(8)));

var result =
    String.Join("\n",
        toBytes("The even quicker brown fox.")
            .Select((b, i) => new { b, i })
            .GroupBy(x => x.i / 16, x => x.b)
            .Select(bs => toHexLine(bs)));
于 2012-05-21T04:51:39.010 に答える
1

LINQ を使用すると、コードが読みやすくなり、使用している IEnumerable の型から切り離されます。ただし、抽象化の性質上、特定のニーズに合わせて下位レベルのコードを手作業で作成するよりも効率が低くなります。

于 2012-05-21T03:56:55.597 に答える
1

これが読みやすいかどうかはわかりませんがBuffer、Reactive Extensions と同様の拡張メソッドを使用するソリューションを次に示します。

public static IEnumerable<IList<T>> Buffer<T>(this IEnumerable<T> orig, int count)
{
    return orig.Select((o,i) => new { o, i })
               .GroupBy(x => x.i / count, x => x.o)
               .Select(g => g.ToList());
}

16 バイトのブロックを指定して、それらを文字列に変換します (各行の末尾に移動します)。

static string FormatAsString(IList<byte> bytes)
{  
    return String.Join(" ", 
                 bytes.Buffer(8).Select(
                     bs => new String(bs.Select(b => ' ' <= b && b <= '~' ? (char)b : '.').ToArray())
                 )
           );
}

バイトのブロック (通常は 16 幅) を指定して、それらをそれらのバイトの文字列表現に変換します (各行の先頭に移動します)。

static string FormatAsBytes(IList<byte> bytes)
{
    var blocks = 
        bytes.Buffer(8)
             .Select(bs => String.Join(" ", 
                bs.Select(b => String.Format("{0:X2}", (int)b)))
             );

    return String.Join("  ", blocks);
}

入力バイトをブロックに変換すると、出力に対して上記の 2 つを実行できます。

static void PrintBytesWide(byte[] bytes, int width)
{
    foreach (var line in bytes.Buffer(width))
    {
        Console.WriteLine("{0} {1}", FormatAsBytes(line).PadRight((width + 1) * 3, ' '), FormatAsString(line));
    }
}

16 バイト ブロックで実行するには:

var bytes = Encoding.UTF8.GetBytes("the quick brown fox");
PrintBytesWide(bytes, 16);

私にとって、これは基本的に元の出力と同じ出力を返しました。

オリジナル:

 74 68 65 20 71 75 69 63  6B 20 62 72 6F 77 6E 20   the quic k brown 
 66 6F 78                                           fox

新しい:

74 68 65 20 71 75 69 63  6B 20 62 72 6F 77 6E 20    the quic k brown 
66 6F 78                                            fox

しかしもちろん、さまざまな幅を作ることができるという利点があります。

PrintBytesWide(bytes, 8);

74 68 65 20 71 75 69 63     the quic
6B 20 62 72 6F 77 6E 20     k brown 
66 6F 78                    fox

PrintBytesWide(bytes, 24);

74 68 65 20 71 75 69 63  6B 20 62 72 6F 77 6E 20  66 6F 78                  the quic k brown  fox
于 2012-05-21T04:20:42.050 に答える