using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Security.Cryptography;
 using System.Text;
 using System.Threading.Tasks;

 namespace democ
 {
    class Program
    {
        static void Main(string[] args)
        {
            string s = GetMD5("12346");
            Console.WriteLine(s);
            Console.ReadKey();
        }
        public static string GetMD5(string str)
        {
            //创建md5
            MD5 md5 = MD5.Create();
            //将字符串转换成字节数组
            byte[] buffer = Encoding.GetEncoding("GBK").GetBytes(str);
            //加密好的字节数组
            byte[] md5buffer=md5.ComputeHash(buffer);
            //字节数组转成字符串
            // return Encoding.GetEncoding("GBK").GetString(md5buffer);
            //将字节数组中每个元素tostring()
            string strNew = "";
            for (int i = 0; i < md5buffer.Length; i++)
            {
                //加上X将十进制转换成十六进制
                strNew += md5buffer[i].ToString("x");
            }
            return strNew;
        }
    }
}

C#tostring()方法一些特殊用法

一、取中文日期显示
1、年月日时分 
       currentTime.ToString("f"); //不显示秒 
2、年月 
       currentTime.ToString("y"); 
3、月日 
       currentTime.ToString("m"); 
4、格式为:2003-9-23 
       currentTime.ToString("d"); 
5、格式为:14:24 
       currentTime.ToString("t"); 

二、字符型转换 转为字符串 
12345.ToString("n"); //结果:12,345.00 
12345.ToString("C");    //结果:¥12,345.00 
12345.ToString("e");    //结果:1.234500e+004 
12345.ToString("f4");    //结果:12345.0000 
12345.ToString("x"); //结果:3039 (16进制) 
12345.ToString("p");    //结果:1,234,500.00%