+1  A: 

This could be any number of encodings... try this test test script to see which of them print out:

Bl8s

Here is the script:

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

Use b.ToString("x2") to format a byte value into a two character hexadecimal string.

For the ASCII display, check if the value corresponds to a regular printable character and convert it if it is:

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

Or shorter:

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

To do it on an array:

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

Try: Encoding.Default.GetBytes

If that doesn't work, there are different types that you can specify (UTF-8, ASCII...)

Jay Mooney
Encoding.Default represents the ANSI code page, and it's clearly not that (being multiple-byte for a start).
Noldorin
+6  A: 

It sounds like you'd like to take an array of bytes, and convert it to text (replacing characters outside of a certain range with "."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();
}

or as a LINQy extension method (inside a static extension class):

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

(You could call that like string printable = byteArray.ToPrintableString();)

Daniel LeCheminant
I think that you mean 0x20 instead of 20. Or use 32 like in the code that I posted.
Guffa
Beware, this function only works for ASCII or ANSI encoding.
Noldorin
Yeah, I caught that; thanks ;-]
Daniel LeCheminant
Wrong code,it should be 0x20(32 decimal).Guffa is right. :)
John
I'm not sure why everyone keeps commenting on the 0x20 thing; I had fixed that almost immediately... is there something else I'm missing here?
Daniel LeCheminant
Yeah,I had forgotten to say "Thanks". :)
John
A: 

If you want the whole memory display, including the offset number, and left and right displays, you can do it like this: (32-byte width)

static public String MemoryDisplay(byte[] mem)
{
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < mem.Length; i += 32)
    {
        byte[] line = mem.Skip(i).Take(32).ToArray();
        String s = String.Format("{0:x8} {1,-96}{2}",
            i,
            String.Join(" ", line.Select(e => e.ToString("x2")).ToArray()),
            new String(line.Select(e => 32 <= e && e <= 127 ? (Char)e : '.').ToArray()));
        sb.AppendLine(s);
    }
    return sb.ToString();
}
Glenn Slayden