欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【C#】常用的一些方法

程序员文章站 2022-06-16 08:16:32
...

    记录一下一些在编程过程中常用的方法

1.获取时间戳    

 public static long GetTimeSpan()
        {
            var time = DateTime.Now;
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new     System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
            return t;
        }

2.Base64编码

  public static string Base64Code(string Message)
        {
            byte[] encData_byte = new byte[Message.Length];
            encData_byte = System.Text.Encoding.UTF8.GetBytes(Message);
            string encodedData = Convert.ToBase64String(encData_byte);
            return encodedData;
        }

3.Md5加密

 public static string Md5Create(string value, string signMethod = "md5")
        {
            // 第三步:使用MD5/HMAC加密
            byte[] bytes;
            if (Constants.SIGN_METHOD_HMAC.Equals(signMethod))
            {
                HMACMD5 hmac = new HMACMD5();
                bytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(value.ToString()));
                hmac.Dispose();
            }
            else
            {
                MD5 md5 = MD5.Create();
                bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(value.ToString()));
                md5.Dispose();
            }

            // 第四步:把二进制转化为大写的十六进制
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                result.Append(bytes[i].ToString("X2"));
            }
            return result.ToString();
        }

 

4.短链接算法(不记得从哪抄的)

  public static string[] ShortUrl(string url)
         {
             //可以自定义生成MD5加密字符传前的混合KEY
             string key = "aleader";
             //要使用生成URL的字符
             string[] chars = new string[]
             {
                 "a", "b", "c", "d", "e", "f", "g", "h",
                 "i", "j", "k", "l", "m", "n", "o", "p",
                 "q", "r", "s", "t", "u", "v", "w", "x",
                 "y", "z", "0", "1", "2", "3", "4", "5",
                 "6", "7", "8", "9", "A", "B", "C", "D",
                 "E", "F", "G", "H", "I", "J", "K", "L",
                 "M", "N", "O", "P", "Q", "R", "S", "T",
                 "U", "V", "W", "X", "Y", "Z"
             };
             //对传入网址进行MD5加密
             string hex = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key + url, "md5");

             string[] resUrl = new string[4];
             for (int i = 0; i< 4; i++)
             {
                 //把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算
                 int hexint = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16);
                 string outChars = string.Empty;
                 for (int j = 0; j< 6; j++)
                 {
                     //把得到的值与0x0000003D进行位与运算,取得字符数组chars索引
                     int index = 0x0000003D & hexint;
                     //把取得的字符相加
                     outChars += chars[index];
                     //每次循环按位右移5位
                     hexint = hexint >> 5;
                 }
                 //把字符串存入对应索引的输出数组
                 resUrl[i] = outChars;
             }
             return resUrl;
         }

5.url编码(对大小写有要求的情况下)


       public static string UrlEncoding(string content)
        {
            var result = new StringBuilder();
            foreach (char c in content)
            {
                var u = HttpUtility.UrlEncode(c.ToString());
                if (u.Length > 1)
                {
                    u=u.ToUpper();
                }
                result.Append(u);
            }
            return result.ToString();
        }

6.HttpGet

7.HttpPost

相关标签: C# 基础 c#