1
0

UtilConvert.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 ToDateTime(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 ToDateTime(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 DateTime ToDateTime(this long milliseconds)
  116. {
  117. return Extensions.DateTimeExtensions.TimestampStart.AddMilliseconds(milliseconds);
  118. }
  119. public static bool ToBool(this object thisValue)
  120. {
  121. bool reval = false;
  122. if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
  123. {
  124. return reval;
  125. }
  126. return reval;
  127. }
  128. public static byte ToByte(this object s)
  129. {
  130. if (s == null || s == DBNull.Value)
  131. return 0;
  132. byte.TryParse(s.ToString(), out byte result);
  133. return result;
  134. }
  135. #region ==字节转换==
  136. /// <summary>
  137. /// 转换为16进制
  138. /// </summary>
  139. /// <param name="bytes"></param>
  140. /// <param name="lowerCase">是否小写</param>
  141. /// <returns></returns>
  142. public static string ToHex(this byte[] bytes, bool lowerCase = true)
  143. {
  144. if (bytes == null)
  145. return null;
  146. var result = new StringBuilder();
  147. var format = lowerCase ? "x2" : "X2";
  148. for (var i = 0; i < bytes.Length; i++)
  149. {
  150. result.Append(bytes[i].ToString(format));
  151. }
  152. return result.ToString();
  153. }
  154. /// <summary>
  155. /// 16进制转字节数组
  156. /// </summary>
  157. /// <param name="s"></param>
  158. /// <returns></returns>
  159. public static byte[] HexToBytes(this string s)
  160. {
  161. if (s.IsNull())
  162. return null;
  163. var bytes = new byte[s.Length / 2];
  164. for (int x = 0; x < s.Length / 2; x++)
  165. {
  166. int i = (Convert.ToInt32(s.Substring(x * 2, 2), 16));
  167. bytes[x] = (byte)i;
  168. }
  169. return bytes;
  170. }
  171. /// <summary>
  172. /// 转换为Base64
  173. /// </summary>
  174. /// <param name="bytes"></param>
  175. /// <returns></returns>
  176. public static string ToBase64(this byte[] bytes)
  177. {
  178. if (bytes == null)
  179. return null;
  180. return Convert.ToBase64String(bytes);
  181. }
  182. #endregion ==字节转换==
  183. }
  184. }