1
0

UtilConvert.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Text;
  3. namespace Admin.Core
  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 = 0;
  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 = 0;
  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 = 0;
  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. public static decimal ToDecimal(this object thisValue)
  65. {
  66. Decimal reval = 0;
  67. if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
  68. {
  69. return reval;
  70. }
  71. return 0;
  72. }
  73. public static decimal ToDecimal(this object thisValue, decimal errorValue)
  74. {
  75. Decimal reval = 0;
  76. if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
  77. {
  78. return reval;
  79. }
  80. return errorValue;
  81. }
  82. public static DateTime ToDate(this object thisValue)
  83. {
  84. DateTime reval = DateTime.MinValue;
  85. if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
  86. {
  87. reval = Convert.ToDateTime(thisValue);
  88. }
  89. return reval;
  90. }
  91. public static DateTime ToDate(this object thisValue, DateTime errorValue)
  92. {
  93. DateTime reval = DateTime.MinValue;
  94. if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
  95. {
  96. return reval;
  97. }
  98. return errorValue;
  99. }
  100. public static bool ToBool(this object thisValue)
  101. {
  102. bool reval = false;
  103. if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
  104. {
  105. return reval;
  106. }
  107. return reval;
  108. }
  109. public static byte ToByte(this object s)
  110. {
  111. if (s == null || s == DBNull.Value)
  112. return 0;
  113. byte.TryParse(s.ToString(), out byte result);
  114. return result;
  115. }
  116. #region ==字节转换==
  117. /// <summary>
  118. /// 转换为16进制
  119. /// </summary>
  120. /// <param name="bytes"></param>
  121. /// <param name="lowerCase">是否小写</param>
  122. /// <returns></returns>
  123. public static string ToHex(this byte[] bytes, bool lowerCase = true)
  124. {
  125. if (bytes == null)
  126. return null;
  127. var result = new StringBuilder();
  128. var format = lowerCase ? "x2" : "X2";
  129. for (var i = 0; i < bytes.Length; i++)
  130. {
  131. result.Append(bytes[i].ToString(format));
  132. }
  133. return result.ToString();
  134. }
  135. /// <summary>
  136. /// 16进制转字节数组
  137. /// </summary>
  138. /// <param name="s"></param>
  139. /// <returns></returns>
  140. public static byte[] HexToBytes(this string s)
  141. {
  142. if (s.IsNull())
  143. return null;
  144. var bytes = new byte[s.Length / 2];
  145. for (int x = 0; x < s.Length / 2; x++)
  146. {
  147. int i = (Convert.ToInt32(s.Substring(x * 2, 2), 16));
  148. bytes[x] = (byte)i;
  149. }
  150. return bytes;
  151. }
  152. /// <summary>
  153. /// 转换为Base64
  154. /// </summary>
  155. /// <param name="bytes"></param>
  156. /// <returns></returns>
  157. public static string ToBase64(this byte[] bytes)
  158. {
  159. if (bytes == null)
  160. return null;
  161. return Convert.ToBase64String(bytes);
  162. }
  163. #endregion
  164. }
  165. }