2010-02-20 180 views
5

這是一個類似於this one here的問題。Builtin函數將字節轉換爲十六進制字符串

是否有一個內置的方法,將字節數組轉換爲十六進制字符串?更具體地說,我正在尋找一個內置的功能

/// <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(); 
    } 

回答

相關問題