开发者

Builtin Function to Convert from Byte to Hex String

This is a question similar to this one here.

Is there a built-in method that converts an array of byte to hex string? More specifically, I'm looking for a built in function for

    /// <summary>
    /// Convert bytes in a array Bytes to string in hexadecimal format
    /// </summary>
    /// <param name="Bytes">Bytes array</param>
    /// <param name="Length">T开发者_StackOverflow中文版otal 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();
    }


Using BitConverter, ref: http://msdn.microsoft.com/en-us/library/bb311038.aspx

byte[] vals = { 0x01, 0xAA, 0xB1, 0xDC, 0x10, 0xDD };

var str = BitConverter.ToString(vals).Replace("-", "");
Console.WriteLine(str);

/*Output:
  01AAB1DC10DD
 */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜