簡體   English   中英

C# - 將字節數組轉換為十六進制字符串的快速方法

[英]C# - Fast Method to Convert Byte Array to Hex String

我想盡快將字節數組轉換為十六進制字符串。

所以通過我之前的問題,我找到了以下代碼

private static readonly uint[] _lookup32 = CreateLookup32();

    private static uint[] CreateLookup32()
    {
        var result = new uint[256];
        for (int i = 0; i < 256; i++)
        {
            string s = i.ToString("X2");
            result[i] = ((uint)s[0]) + ((uint)s[1] << 16);
        }
        return result;
    }

    private static string ByteArrayToHexViaLookup32(byte[] bytes)
    {
        var lookup32 = _lookup32;
        var result = new char[bytes.Length * 2];
        for (int i = 0; i < bytes.Length; i++)
        {
            var val = lookup32[bytes[i]];
            result[2 * i] = (char)val;
            result[2 * i + 1] = (char)(val >> 16);
        }
        return new string(result);
    }

這很好用,但問題在於輸出字符串如下所示:

output: 0F42000AAD24120024
but i need it like this: 0F 42 00 0A AD 24 12 00 24

由於我的編碼知識對“神秘”的算法有點熟悉,我不知道在哪里以及如何添加代碼,因此它會在每個 2 個字節之間添加一個空格 - (Hexoutputstring +“”)。

我可以循環遍歷字符串並每 2 個字符添加一個空格,但這會大大增加它需要的時間來提供有用的結果,因為附加字符串很慢。

有人可以幫我上面的代碼嗎? 謝謝 :)

    private static string ByteArrayToHexViaLookup32(byte[] bytes)
    {
        var lookup32 = _lookup32;
        var byteCount = bytes.Length;
        var result = new char[3* byteCount - 1];
        for (int i = 0; i < byteCount; i++)
        {
            var val = lookup32[bytes[i]];
            int index = 2 * i;
            result[index] = (char)val;
            result[index + 1] = (char)(val >> 16);
            if (i < byteCount - 1) result[index + 2] = ' ';
        }
        return new string(result);
    }

如果性能是您的主要關注點之一,我會采用以下方法:

private static readonly char[] digits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

private static string ByteArrayToHexViaLookup32(byte[] bytes)
{
    char[] buffer = new char[bytes.Length * 3];
    
    int index = 0;
    for (int i = 0; i < bytes.Length; i++)
    {
        if (index > 0)
            buffer[index++] = ' ';

        buffer[index++] = digits[(bytes[i] >> 4) & 0xf];
        buffer[index++] = digits[bytes[i] & 0xf];
    }
    
    return new string(buffer, 0, index);
}

以下版本不需要任何查找數組,但我不確定它是否一樣快。

private static string ByteArrayToHexViaLookup32(byte[] bytes)
{
    char[] buffer = new char[bytes.Length * 3];

    int index = 0;
    for (int i = 0; i < bytes.Length; i++)
    {
        if (index > 0)
            buffer[index++] = ' ';

        buffer[index++] = GetDigit((bytes[i] >> 4) & 0xf);
        buffer[index++] = GetDigit(bytes[i] & 0xf);
    }

    return new string(buffer, 0, index);
}

private char GetDigit(int value)
{
    if (value < 10)
        return (char)('0' + value);
    return (char)('7' + value);
}

兩個版本都在字節之間插入一個空格。

private static string ByteArrayToStringHex(byte[] bytes)
    {
        string hexValue = BitConverter.ToString(bytes);
        hexValue = hexValue.Replace("-", " ");

        return hexValue;
    }

我認為它的結果與您想要的值相同

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM