convert string to hexadecimal

1. Convert ordinary strings to hexadecimal strings. ,

class Program
{
    static void Main(string[] args)
    {
        string str = "Hello,C#!!!";
        //调用。
        string result = StringToHex16(str);
        Console.WriteLine( string .Format( " Convert ordinary string: {0} to hexadecimal string: {1} " , str, result));
        Console.ReadKey();
    }    
    ///  <summary> 
    /// This method is used to convert ordinary strings into hexadecimal strings.
    ///  </summary> 
    ///  <param name="_str"> The string to convert. </param> 
    ///  <returns></returns> 
    public  static  string StringToHex16( string _str)
    {
        // Convert string to byte array. 
        byte [] buffer = System.Text.Encoding.UTF8.GetBytes(_str);
         // Define a variable of type string to store the converted value. 
        string result = string .Empty;
         for ( int i = 0 ; i < buffer.Length; i++ )
        {
            // Convert each byte array into a hexadecimal string, separated by spaces. 
            result += Convert.ToString(buffer[i], 16 ) + "  " ;
        }
        return result;
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325043204&siteId=291194637