CommonUtil.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. namespace SKMC.Api.Common
  8. {
  9. /// <summary>
  10. /// 字符串等数值变换工具类
  11. /// </summary>
  12. public class CommonUtil
  13. {
  14. /// <summary>
  15. /// 创建一个基于Guid的long类型Id
  16. /// </summary>
  17. /// <returns></returns>
  18. public static long LongId()
  19. {
  20. byte[] guidBytes = Guid.NewGuid().ToByteArray();
  21. return BitConverter.ToInt64(guidBytes, 0) & long.MaxValue;
  22. }
  23. /// <summary>
  24. /// 生成一个基于Guid连续字符的字符串Id
  25. /// </summary>
  26. /// <returns></returns>
  27. public static string StringId()
  28. {
  29. return Guid.NewGuid().ToString("N");
  30. }
  31. /// <summary>
  32. /// String类型変換
  33. /// </summary>
  34. /// <param name="value">目标对象</param>
  35. /// <returns></returns>
  36. public static string GetStringValue(object value)
  37. {
  38. return GetStringValue(value, string.Empty);
  39. }
  40. /// <summary>
  41. /// String类型変換
  42. /// </summary>
  43. /// <param name="value">目标对象</param>
  44. /// <param name="defaultValue">默认值</param>
  45. /// <returns></returns>
  46. public static string GetStringValue(object value, string defaultValue)
  47. {
  48. if (Convert.IsDBNull(value))
  49. {
  50. return defaultValue;
  51. }
  52. else if (value == null)
  53. {
  54. return defaultValue;
  55. }
  56. else
  57. {
  58. return value.ToString();
  59. }
  60. }
  61. /// <summary>
  62. /// Bool类型変換
  63. /// </summary>
  64. /// <param name="value">目标对象</param>
  65. /// <returns></returns>
  66. public static bool GetBoolValue(string value)
  67. {
  68. bool bValue;
  69. Boolean.TryParse(value, out bValue);
  70. return bValue;
  71. }
  72. /// <summary>
  73. /// Int类型変換
  74. /// </summary>
  75. /// <param name="value">目标对象</param>
  76. /// <returns></returns>
  77. public static int GetIntValue(object value)
  78. {
  79. return CommonUtil.GetIntValue(value, 0);
  80. }
  81. /// <summary>
  82. /// Int类型変換
  83. /// </summary>
  84. /// <param name="value">目标对象</param>
  85. /// <param name="defaultValue">默认值</param>
  86. /// <returns></returns>
  87. public static int GetIntValue(object value, int defaultValue)
  88. {
  89. if (Convert.IsDBNull(value))
  90. {
  91. return defaultValue;
  92. }
  93. else if (value == null)
  94. {
  95. return defaultValue;
  96. }
  97. int result;
  98. if (int.TryParse(value.ToString(), out result) == true)
  99. {
  100. return result;
  101. }
  102. else
  103. {
  104. return defaultValue;
  105. }
  106. }
  107. /// <summary>
  108. /// Float类型変換
  109. /// </summary>
  110. /// <param name="value">目标对象</param>
  111. /// <returns></returns>
  112. public static float GetFloatValue(object value)
  113. {
  114. return GetFloatValue(value, 0);
  115. }
  116. /// <summary>
  117. /// Float类型変換
  118. /// </summary>
  119. /// <param name="data">目标对象</param>
  120. /// <returns></returns>
  121. public static float GetNotNullFloatValue(Nullable<float> data)
  122. {
  123. if (data != null)
  124. {
  125. return GetFloatValue(data.Value, 0);
  126. }
  127. else
  128. {
  129. return 0;
  130. }
  131. }
  132. /// <summary>
  133. /// Float类型変換
  134. /// </summary>
  135. /// <param name="value">目标对象</param>
  136. /// <param name="defaultValue">默认值</param>
  137. /// <returns></returns>
  138. public static float GetFloatValue(object value, float defaultValue)
  139. {
  140. if (Convert.IsDBNull(value))
  141. {
  142. return defaultValue;
  143. }
  144. else if (value == null)
  145. {
  146. return defaultValue;
  147. }
  148. float result;
  149. if (float.TryParse(value.ToString(), out result) == true)
  150. {
  151. return result;
  152. }
  153. else
  154. {
  155. return defaultValue;
  156. }
  157. }
  158. /// <summary>
  159. /// 比较两个字符串类型的值是否相同
  160. /// </summary>
  161. /// <param name="value1">字符串1</param>
  162. /// <param name="value2">字符串2</param>
  163. /// <returns></returns>
  164. public static bool StringCompare(string value1, string value2)
  165. {
  166. bool result;
  167. if ((value1 == null) && (value2 == null))
  168. {
  169. result = true;
  170. }
  171. if ((value1 == null) || (value2 == null))
  172. {
  173. result = false;
  174. }
  175. else
  176. {
  177. result = value1.Equals(value2);
  178. }
  179. return result;
  180. }
  181. /// <summary>
  182. /// 确认指定的字符串是否为空
  183. /// </summary>
  184. /// <param name="value">対象文字列</param>
  185. /// <returns></returns>
  186. public static bool IsEmptyString(string value)
  187. {
  188. if (value == null)
  189. {
  190. return true;
  191. }
  192. if (StringCompare(value.Trim(), string.Empty) == true)
  193. {
  194. return true;
  195. }
  196. return false;
  197. }
  198. public static bool IsEmptyList<T>(IEnumerable<T> source)
  199. {
  200. if (source == null) return true;
  201. return !source.Any();
  202. }
  203. /// <summary>
  204. /// 小数标记验证
  205. /// </summary>
  206. /// <param name="target">验证对象</param>
  207. /// <param name="intDigit">整数位数</param>
  208. /// <param name="decDigit">小数位数</param>
  209. /// <returns></returns>
  210. public static bool IsDecimalNumber(object target, int intDigit, int decDigit)
  211. {
  212. bool rlt = false;
  213. if (intDigit > 0 && decDigit > 0 && IsDecimal(GetStringValue(target)))
  214. {
  215. StringBuilder regularExp = new StringBuilder();
  216. regularExp.Append("^\\-?[0-9]{1,");
  217. regularExp.Append(intDigit);
  218. regularExp.Append("}(\\.[0-9]{1,");
  219. regularExp.Append(decDigit);
  220. regularExp.Append("})?$");
  221. if (Regex.IsMatch(target.ToString(), regularExp.ToString()))
  222. {
  223. rlt = true;
  224. }
  225. }
  226. return rlt;
  227. }
  228. /// <summary>
  229. /// 是否为Decimal
  230. /// </summary>
  231. /// <param name="target"></param>
  232. /// <returns></returns>
  233. public static bool IsDecimal(string target)
  234. {
  235. bool result = false;
  236. decimal decNum;
  237. if (Decimal.TryParse(target, out decNum))
  238. {
  239. result = true;
  240. }
  241. else
  242. {
  243. result = false;
  244. }
  245. return result;
  246. }
  247. /// <summary>
  248. /// double类型変換
  249. /// </summary>
  250. /// <param name="value">目标对象</param>
  251. /// <returns></returns>
  252. public static double GetDoubleValue(object value)
  253. {
  254. return GetDoubleValue(value, 0);
  255. }
  256. /// <summary>
  257. /// double类型変換
  258. /// </summary>
  259. /// <param name="value">目标对象</param>
  260. /// <param name="defaultValue">默认值</param>
  261. /// <returns></returns>
  262. public static double GetDoubleValue(object value, double defaultValue)
  263. {
  264. if (Convert.IsDBNull(value))
  265. {
  266. return defaultValue;
  267. }
  268. else if (value == null)
  269. {
  270. return defaultValue;
  271. }
  272. double result;
  273. if (double.TryParse(value.ToString(), out result) == true)
  274. {
  275. return result;
  276. }
  277. else
  278. {
  279. return defaultValue;
  280. }
  281. }
  282. /// <summary>
  283. /// 舍入小于或等于默认小数
  284. /// </summary>
  285. /// <param name="value">対象数值</param>
  286. /// <param name="format">显示小数位数格式({0:##0.000})</param>
  287. /// <returns>結果</returns>
  288. public static float CutDecimalValue(float value, string format)
  289. {
  290. string strNewWithoutDec = string.Format(format, value);
  291. return GetFloatValue(strNewWithoutDec);
  292. }
  293. /// <summary>
  294. /// 将数字转换为Byte数组(翻转)
  295. /// </summary>
  296. /// <param name="data">数值数据</param>
  297. /// <returns>byte数组</returns>
  298. public static byte[] GetIntReversedByteValue(int data, int len)
  299. {
  300. byte[] inValue = BitConverter.GetBytes(data);
  301. byte[] retValue = new byte[len];
  302. for (int i = 0; i < len; i++)
  303. {
  304. retValue[i] = (i < inValue.Length) ? inValue[i] : (byte)0;
  305. }
  306. // 翻转
  307. Array.Reverse(retValue);
  308. return retValue;
  309. }
  310. /// <summary>
  311. /// 将数字转换为Byte数组
  312. /// </summary>
  313. /// <param name="data">数值数据</param>
  314. /// <returns>byte数组</returns>
  315. public static byte[] GetIntByteValue(int data, int len)
  316. {
  317. byte[] inValue = BitConverter.GetBytes(data);
  318. byte[] retValue = new byte[len];
  319. for (int i = 0; i < len; i++)
  320. {
  321. retValue[i] = (i < inValue.Length) ? inValue[i] : (byte)0;
  322. }
  323. return retValue;
  324. }
  325. public static string PasswordEncryption(string pwd)
  326. {
  327. SHA1 sha1 = SHA1.Create();
  328. byte[] originalPwd = Encoding.UTF8.GetBytes(pwd);
  329. byte[] encryPwd = sha1.ComputeHash(originalPwd);
  330. return string.Join("", encryPwd.Select(b => string.Format("{0:x2}", b)).ToArray()).ToUpper();
  331. }
  332. }
  333. }