UtilConvertExtensions.cs 6.1 KB

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