将字节转换为十六进制字符串的内置函数

5

这个问题类似于这里的问题

是否有一种内置方法将字节数组转换为十六进制字符串?更具体地说,我正在寻找一个内置函数来进行:

    /// <summary>
    /// Convert bytes in a array Bytes to string in hexadecimal format
    /// </summary>
    /// <param name="Bytes">Bytes array</param>
    /// <param name="Length">Total byte to convert</param>
    /// <returns></returns>
    public static string ByteToHexString(byte[] Bytes, int Length)
    {
        Debug.Assert(Length <= Bytes.GetLength(0));
        StringBuilder hexstr = new StringBuilder();

        for (int i = 0; i < Length; i++)
        {
            hexstr.AppendFormat("{0,02:X}", Bytes[i]);
        }

        hexstr.Replace(' ', '0'); //padd empty space to zero

        return hexstr.ToString();
    }
1个回答

4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接