| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace SKMC.Api.Common
- {
- /// <summary>
- /// 字符串等数值变换工具类
- /// </summary>
- public class CommonUtil
- {
- /// <summary>
- /// 创建一个基于Guid的long类型Id
- /// </summary>
- /// <returns></returns>
- public static long LongId()
- {
- byte[] guidBytes = Guid.NewGuid().ToByteArray();
- return BitConverter.ToInt64(guidBytes, 0) & long.MaxValue;
- }
- /// <summary>
- /// 生成一个基于Guid连续字符的字符串Id
- /// </summary>
- /// <returns></returns>
- public static string StringId()
- {
- return Guid.NewGuid().ToString("N");
- }
- /// <summary>
- /// String类型変換
- /// </summary>
- /// <param name="value">目标对象</param>
- /// <returns></returns>
- public static string GetStringValue(object value)
- {
- return GetStringValue(value, string.Empty);
- }
- /// <summary>
- /// String类型変換
- /// </summary>
- /// <param name="value">目标对象</param>
- /// <param name="defaultValue">默认值</param>
- /// <returns></returns>
- public static string GetStringValue(object value, string defaultValue)
- {
- if (Convert.IsDBNull(value))
- {
- return defaultValue;
- }
- else if (value == null)
- {
- return defaultValue;
- }
- else
- {
- return value.ToString();
- }
- }
- /// <summary>
- /// Bool类型変換
- /// </summary>
- /// <param name="value">目标对象</param>
- /// <returns></returns>
- public static bool GetBoolValue(string value)
- {
- bool bValue;
- Boolean.TryParse(value, out bValue);
- return bValue;
- }
- /// <summary>
- /// Int类型変換
- /// </summary>
- /// <param name="value">目标对象</param>
- /// <returns></returns>
- public static int GetIntValue(object value)
- {
- return CommonUtil.GetIntValue(value, 0);
- }
- /// <summary>
- /// Int类型変換
- /// </summary>
- /// <param name="value">目标对象</param>
- /// <param name="defaultValue">默认值</param>
- /// <returns></returns>
- public static int GetIntValue(object value, int defaultValue)
- {
- if (Convert.IsDBNull(value))
- {
- return defaultValue;
- }
- else if (value == null)
- {
- return defaultValue;
- }
- int result;
- if (int.TryParse(value.ToString(), out result) == true)
- {
- return result;
- }
- else
- {
- return defaultValue;
- }
- }
- /// <summary>
- /// Float类型変換
- /// </summary>
- /// <param name="value">目标对象</param>
- /// <returns></returns>
- public static float GetFloatValue(object value)
- {
- return GetFloatValue(value, 0);
- }
- /// <summary>
- /// Float类型変換
- /// </summary>
- /// <param name="data">目标对象</param>
- /// <returns></returns>
- public static float GetNotNullFloatValue(Nullable<float> data)
- {
- if (data != null)
- {
- return GetFloatValue(data.Value, 0);
- }
- else
- {
- return 0;
- }
- }
- /// <summary>
- /// Float类型変換
- /// </summary>
- /// <param name="value">目标对象</param>
- /// <param name="defaultValue">默认值</param>
- /// <returns></returns>
- public static float GetFloatValue(object value, float defaultValue)
- {
- if (Convert.IsDBNull(value))
- {
- return defaultValue;
- }
- else if (value == null)
- {
- return defaultValue;
- }
- float result;
- if (float.TryParse(value.ToString(), out result) == true)
- {
- return result;
- }
- else
- {
- return defaultValue;
- }
- }
- /// <summary>
- /// 比较两个字符串类型的值是否相同
- /// </summary>
- /// <param name="value1">字符串1</param>
- /// <param name="value2">字符串2</param>
- /// <returns></returns>
- public static bool StringCompare(string value1, string value2)
- {
- bool result;
- if ((value1 == null) && (value2 == null))
- {
- result = true;
- }
- if ((value1 == null) || (value2 == null))
- {
- result = false;
- }
- else
- {
- result = value1.Equals(value2);
- }
- return result;
- }
- /// <summary>
- /// 确认指定的字符串是否为空
- /// </summary>
- /// <param name="value">対象文字列</param>
- /// <returns></returns>
- public static bool IsEmptyString(string value)
- {
- if (value == null)
- {
- return true;
- }
- if (StringCompare(value.Trim(), string.Empty) == true)
- {
- return true;
- }
- return false;
- }
- public static bool IsEmptyList<T>(IEnumerable<T> source)
- {
- if (source == null) return true;
- return !source.Any();
- }
- /// <summary>
- /// 小数标记验证
- /// </summary>
- /// <param name="target">验证对象</param>
- /// <param name="intDigit">整数位数</param>
- /// <param name="decDigit">小数位数</param>
- /// <returns></returns>
- public static bool IsDecimalNumber(object target, int intDigit, int decDigit)
- {
- bool rlt = false;
- if (intDigit > 0 && decDigit > 0 && IsDecimal(GetStringValue(target)))
- {
- StringBuilder regularExp = new StringBuilder();
- regularExp.Append("^\\-?[0-9]{1,");
- regularExp.Append(intDigit);
- regularExp.Append("}(\\.[0-9]{1,");
- regularExp.Append(decDigit);
- regularExp.Append("})?$");
- if (Regex.IsMatch(target.ToString(), regularExp.ToString()))
- {
- rlt = true;
- }
- }
- return rlt;
- }
- /// <summary>
- /// 是否为Decimal
- /// </summary>
- /// <param name="target"></param>
- /// <returns></returns>
- public static bool IsDecimal(string target)
- {
- bool result = false;
- decimal decNum;
- if (Decimal.TryParse(target, out decNum))
- {
- result = true;
- }
- else
- {
- result = false;
- }
- return result;
- }
- /// <summary>
- /// double类型変換
- /// </summary>
- /// <param name="value">目标对象</param>
- /// <returns></returns>
- public static double GetDoubleValue(object value)
- {
- return GetDoubleValue(value, 0);
- }
- /// <summary>
- /// double类型変換
- /// </summary>
- /// <param name="value">目标对象</param>
- /// <param name="defaultValue">默认值</param>
- /// <returns></returns>
- public static double GetDoubleValue(object value, double defaultValue)
- {
- if (Convert.IsDBNull(value))
- {
- return defaultValue;
- }
- else if (value == null)
- {
- return defaultValue;
- }
- double result;
- if (double.TryParse(value.ToString(), out result) == true)
- {
- return result;
- }
- else
- {
- return defaultValue;
- }
- }
- /// <summary>
- /// 舍入小于或等于默认小数
- /// </summary>
- /// <param name="value">対象数值</param>
- /// <param name="format">显示小数位数格式({0:##0.000})</param>
- /// <returns>結果</returns>
- public static float CutDecimalValue(float value, string format)
- {
- string strNewWithoutDec = string.Format(format, value);
- return GetFloatValue(strNewWithoutDec);
- }
- /// <summary>
- /// 将数字转换为Byte数组(翻转)
- /// </summary>
- /// <param name="data">数值数据</param>
- /// <returns>byte数组</returns>
- public static byte[] GetIntReversedByteValue(int data, int len)
- {
- byte[] inValue = BitConverter.GetBytes(data);
- byte[] retValue = new byte[len];
- for (int i = 0; i < len; i++)
- {
- retValue[i] = (i < inValue.Length) ? inValue[i] : (byte)0;
- }
- // 翻转
- Array.Reverse(retValue);
- return retValue;
- }
- /// <summary>
- /// 将数字转换为Byte数组
- /// </summary>
- /// <param name="data">数值数据</param>
- /// <returns>byte数组</returns>
- public static byte[] GetIntByteValue(int data, int len)
- {
- byte[] inValue = BitConverter.GetBytes(data);
- byte[] retValue = new byte[len];
- for (int i = 0; i < len; i++)
- {
- retValue[i] = (i < inValue.Length) ? inValue[i] : (byte)0;
- }
- return retValue;
- }
- public static string PasswordEncryption(string pwd)
- {
- SHA1 sha1 = SHA1.Create();
- byte[] originalPwd = Encoding.UTF8.GetBytes(pwd);
- byte[] encryPwd = sha1.ComputeHash(originalPwd);
- return string.Join("", encryPwd.Select(b => string.Format("{0:x2}", b)).ToArray()).ToUpper();
- }
- }
- }
|