UtilConvert.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Text;
  3. namespace Admin.Core.Common.Helpers
  4. {
  5. /// <summary>
  6. /// 数据类型转换
  7. /// </summary>
  8. public static class UtilConvert
  9. {
  10. public static int ToInt(this object thisValue)
  11. {
  12. int reval = 0;
  13. if (thisValue == null) return 0;
  14. if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
  15. {
  16. return reval;
  17. }
  18. return reval;
  19. }
  20. public static int ToInt(this object thisValue, int errorValue)
  21. {
  22. int reval;
  23. if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
  24. {
  25. return reval;
  26. }
  27. return errorValue;
  28. }
  29. public static long ToLong(this object s)
  30. {
  31. if (s == null || s == DBNull.Value)
  32. return 0L;
  33. long.TryParse(s.ToString(), out long result);
  34. return result;
  35. }
  36. public static double ToMoney(this object thisValue)
  37. {
  38. double reval;
  39. if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
  40. {
  41. return reval;
  42. }
  43. return 0;
  44. }
  45. public static double ToMoney(this object thisValue, double errorValue)
  46. {
  47. double reval;
  48. if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
  49. {
  50. return reval;
  51. }
  52. return errorValue;
  53. }
  54. public static string ToString(this object thisValue)
  55. {
  56. if (thisValue != null) return thisValue.ToString().Trim();
  57. return "";
  58. }
  59. public static string ToString(this object thisValue, string errorValue)
  60. {
  61. if (thisValue != null) return thisValue.ToString().Trim();
  62. return errorValue;
  63. }
  64. /// <summary>
  65. /// 转换成Double/Single
  66. /// </summary>
  67. /// <param name="s"></param>
  68. /// <param name="digits">小数位数</param>
  69. /// <returns></returns>
  70. public static double ToDouble(this object s, int? digits = null)
  71. {
  72. if (s == null || s == DBNull.Value)
  73. return 0d;
  74. double.TryParse(s.ToString(), out double result);
  75. if (digits == null)
  76. return result;
  77. return Math.Round(result, digits.Value);
  78. }
  79. public static decimal ToDecimal(this object thisValue)
  80. {
  81. decimal reval;
  82. if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
  83. {
  84. return reval;
  85. }
  86. return 0;
  87. }
  88. public static decimal ToDecimal(this object thisValue, decimal errorValue)
  89. {
  90. decimal reval;
  91. if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
  92. {
  93. return reval;
  94. }
  95. return errorValue;
  96. }
  97. public static DateTime ToDate(this object thisValue)
  98. {
  99. DateTime reval = DateTime.MinValue;
  100. if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
  101. {
  102. reval = Convert.ToDateTime(thisValue);
  103. }
  104. return reval;
  105. }
  106. public static DateTime ToDate(this object thisValue, DateTime errorValue)
  107. {
  108. DateTime reval;
  109. if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
  110. {
  111. return reval;
  112. }
  113. return errorValue;
  114. }
  115. public static bool ToBool(this object thisValue)
  116. {
  117. bool reval = false;
  118. if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
  119. {
  120. return reval;
  121. }
  122. return reval;
  123. }
  124. public static byte ToByte(this object s)
  125. {
  126. if (s == null || s == DBNull.Value)
  127. return 0;
  128. byte.TryParse(s.ToString(), out byte result);
  129. return result;
  130. }
  131. #region ==字节转换==
  132. /// <summary>
  133. /// 转换为16进制
  134. /// </summary>
  135. /// <param name="bytes"></param>
  136. /// <param name="lowerCase">是否小写</param>
  137. /// <returns></returns>
  138. public static string ToHex(this byte[] bytes, bool lowerCase = true)
  139. {
  140. if (bytes == null)
  141. return null;
  142. var result = new StringBuilder();
  143. var format = lowerCase ? "x2" : "X2";
  144. for (var i = 0; i < bytes.Length; i++)
  145. {
  146. result.Append(bytes[i].ToString(format));
  147. }
  148. return result.ToString();
  149. }
  150. /// <summary>
  151. /// 16进制转字节数组
  152. /// </summary>
  153. /// <param name="s"></param>
  154. /// <returns></returns>
  155. public static byte[] HexToBytes(this string s)
  156. {
  157. if (s.IsNull())
  158. return null;
  159. var bytes = new byte[s.Length / 2];
  160. for (int x = 0; x < s.Length / 2; x++)
  161. {
  162. int i = (Convert.ToInt32(s.Substring(x * 2, 2), 16));
  163. bytes[x] = (byte)i;
  164. }
  165. return bytes;
  166. }
  167. /// <summary>
  168. /// 转换为Base64
  169. /// </summary>
  170. /// <param name="bytes"></param>
  171. /// <returns></returns>
  172. public static string ToBase64(this byte[] bytes)
  173. {
  174. if (bytes == null)
  175. return null;
  176. return Convert.ToBase64String(bytes);
  177. }
  178. #endregion
  179. }
  180. }