我可以说我不知道我在问什么帮助,因为我不知道的格式,但我已经有了一个图片。

我有一个字节[]array,我怎么将它转换成这种形式下(在正确的)?

alt文本http://img512.imageshack.us/img512/3548/48667724.jpg

它不是普通的ascii。

有帮助吗?

解决方案

使用 b.ToString("x2") 格式化的一个字节的数值变成一个两个字hexadecimal string.

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

其他提示

这听起来像你想采取一系列字节,并且将其转换为文本(替换人物以外的某些范围与"."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.......;

尝试:编码。默认。GetBytes

如果这不起作用,有不同类型的你可以指定(UTF-8,ASCII...)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top