码迷,mamicode.com
首页 > 其他好文 > 详细

工作中常用的方法

时间:2015-02-12 12:12:43      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

1.MD5函数

        public static string MD5(string str)    

        {         

           byte[] b = Encoding.UTF8.GetBytes(str);  

           b = new MD5CryptoServiceProvider().ComputeHash(b);

            string ret = "";         

            for (int i = 0; i < b.Length; i++)     

        {              

          ret += b[i].ToString("x").PadLeft(2, ‘0‘);     

        }

            return ret;  

        }

2.SHA256函数

         public static string SHA256(string str)
        {
            byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
            SHA256Managed Sha256 = new SHA256Managed();
            byte[] Result = Sha256.ComputeHash(SHA256Data);
            return Convert.ToBase64String(Result);  //返回长度为44字节的字符串
         }

3.将指定日期转换成yyyy年MM月dd日形式

       

public static string SubLastThreeZero(Object strNumber)       

  {           

  if (strNumber != null && !string.IsNullOrEmpty(strNumber.ToString().Trim()))     

        {              

   string[] Nums = strNumber.ToString().Trim().Split(‘.‘);         

        if (Nums.Length == 2)        

         {             

        Byte[] NumBytes = Encoding.ASCII.GetBytes(Nums[1]);   

                  Char[] NumChars = Encoding.ASCII.GetChars(NumBytes);

                    ArrayList arr = new ArrayList(NumChars);         

            for (int i = arr.Count; i > 0; i--)           

          {                     

    if (arr[i - 1].ToString() == "0")             

            {                      

       arr.RemoveAt(i - 1);            

             }           

              else            

             {                      

       break;                

         }                

     }             

        if (arr.Count > 0)      

               {                

         string Num1 = "";             

            for (int i = 0; i < arr.Count; i++)      

                   {                   

          Num1 += arr[i];

                        }              

           return Nums[0] + "." + Num1;      

               }                

     else                   

  {                   

      return Nums[0];      

               }           

      }            

     return strNumber.ToString();

            }       

      return "";   

      }

 

工作中常用的方法

标签:

原文地址:http://www.cnblogs.com/Yunshine-sina/p/4287516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!