From e80b8b61d3a5428a53bc53fca2b8e4add6626a5d Mon Sep 17 00:00:00 2001 From: Sheldon-NULL <83170675+Sheldon-NULL@users.noreply.github.com> Date: Wed, 25 Oct 2023 09:43:32 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=20=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E8=BD=AC=E5=8C=96=E6=8B=93=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConvertCategory/Extension.Convert.cs | 367 ++++++++++++++++++ 1 file changed, 367 insertions(+) create mode 100644 EasyTool.Core/ConvertCategory/Extension.Convert.cs diff --git a/EasyTool.Core/ConvertCategory/Extension.Convert.cs b/EasyTool.Core/ConvertCategory/Extension.Convert.cs new file mode 100644 index 0000000..c2e380c --- /dev/null +++ b/EasyTool.Core/ConvertCategory/Extension.Convert.cs @@ -0,0 +1,367 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.ConvertCategory +{ + /// + /// 数据类型转化 + /// + public static partial class Extension + { + + #region ==数据转换扩展== + + /// + /// 转换成Byte + /// + /// 输入字符串 + /// + public static byte ToByte(this object s) + { + if (s == null || s == DBNull.Value) + return 0; + + byte.TryParse(s.ToString(), out byte result); + return result; + } + + /// + /// 转换成short/Int16 + /// + /// + /// + public static short ToShort(this object s) + { + if (s == null || s == DBNull.Value) + return 0; + + short.TryParse(s.ToString(), out short result); + return result; + } + + /// + /// 转换成Int/Int32 + /// + /// + /// 是否四舍五入,默认false + /// + public static int ToInt(this object s, bool round = false) + { + if (s == null || s == DBNull.Value) + return 0; + + if (s.GetType().IsEnum) + { + return (int)s; + } + + if (s is bool b) + return b ? 1 : 0; + + if (int.TryParse(s.ToString(), out int result)) + return result; + + var f = s.ToFloat(); + return round ? Convert.ToInt32(f) : (int)f; + } + + /// + /// 转换成Long/Int64 + /// + /// + /// + public static long ToLong(this object s) + { + if (s == null || s == DBNull.Value) + return 0L; + + long.TryParse(s.ToString(), out long result); + return result; + } + + /// + /// 转换成Float/Single + /// + /// + /// 小数位数 + /// + public static float ToFloat(this object s, int? decimals = null) + { + if (s == null || s == DBNull.Value) + return 0f; + + float.TryParse(s.ToString(), out float result); + + if (decimals == null) + return result; + + return (float)Math.Round(result, decimals.Value); + } + + /// + /// 转换成Double/Single + /// + /// + /// 小数位数 + /// + public static double ToDouble(this object s, int? digits = null) + { + if (s == null || s == DBNull.Value) + return 0d; + + double.TryParse(s.ToString(), out double result); + + if (digits == null) + return result; + + return Math.Round(result, digits.Value); + } + + /// + /// 转换成Decimal + /// + /// + /// 小数位数 + /// + public static decimal ToDecimal(this object s, int? decimals = null) + { + if (s == null || s == DBNull.Value) return 0m; + + decimal.TryParse(s.ToString(), out decimal result); + + if (decimals == null) + return result; + + return Math.Round(result, decimals.Value); + } + + /// + /// 转换成DateTime + /// + /// + /// + public static DateTime ToDateTime(this object s) + { + if (s == null || s == DBNull.Value) + return DateTime.MinValue; + + DateTime.TryParse(s.ToString(), out DateTime result); + return result; + } + + /// + /// 转换成Date + /// + /// + /// + public static DateTime ToDate(this object s) + { + return s.ToDateTime().Date; + } + + /// + /// 转换成Boolean + /// + /// + /// + public static bool ToBool(this object s) + { + if (s == null) return false; + s = s.ToString().ToLower(); + if (s.Equals(1) || s.Equals("1") || s.Equals("true") || s.Equals("是") || s.Equals("yes")) + return true; + if (s.Equals(0) || s.Equals("0") || s.Equals("false") || s.Equals("否") || s.Equals("no")) + return false; + + Boolean.TryParse(s.ToString(), out bool result); + return result; + } + + /// + /// 泛型转换,转换失败会抛出异常 + /// + /// + /// + /// + public static T To(this object s) + { + return (T)Convert.ChangeType(s, typeof(T)); + } + + #endregion + + #region ==布尔转换== + + /// + /// 布尔值转换为字符串1或者0 + /// + /// + /// + public static string ToIntString(this bool b) + { + return b ? "1" : "0"; + } + + /// + /// 布尔值转换为整数1或者0 + /// + /// + /// + public static int ToInt(this bool b) + { + return b ? 1 : 0; + } + + /// + /// 布尔值转换为中文 + /// + /// + /// + public static string ToZhCn(this bool b) + { + return b ? "是" : "否"; + } + + #endregion + + #region ==字节转换== + + /// + /// 转换为16进制 + /// + /// + /// 是否小写 + /// + public static string ToHex(this byte[] bytes, bool lowerCase = true) + { + if (bytes == null) + return string.Empty; + + var result = new StringBuilder(); + var format = lowerCase ? "x2" : "X2"; + for (var i = 0; i < bytes.Length; i++) + { + result.Append(bytes[i].ToString(format)); + } + + return result.ToString(); + } + + /// + /// 16进制转字节数组 + /// + /// + /// + public static byte[]? HexToBytes(this string s) + { + if (string.IsNullOrWhiteSpace(s)) + return null; + var bytes = new byte[s.Length / 2]; + + for (int x = 0; x < s.Length / 2; x++) + { + int i = (Convert.ToInt32(s.Substring(x * 2, 2), 16)); + bytes[x] = (byte)i; + } + + return bytes; + } + + /// + /// 转换为Base64 + /// + /// + /// + public static string ToBase64(this byte[] bytes) + { + if (bytes == null) + return string.Empty; + + return Convert.ToBase64String(bytes); + } + + + + /// + /// 时间戳转本时区日期时间 + /// + /// + /// + public static DateTime ConvertFromDateTimeOffset(this DateTimeOffset dateTime) + { + if (dateTime.Offset.Equals(TimeSpan.Zero)) + return dateTime.UtcDateTime; + else if (dateTime.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(dateTime.DateTime))) + return DateTime.SpecifyKind(dateTime.DateTime, DateTimeKind.Local); + else + return dateTime.DateTime; + } + + /// + /// 时间戳转本时区日期时间 + /// + /// + /// + public static DateTime TimestampToDateTime(this string timeStamp) + { + DateTime dd = DateTime.SpecifyKind(new DateTime(1970, 1, 1, 0, 0, 0, 0), DateTimeKind.Local); + long longTimeStamp = long.Parse(timeStamp + "0000"); + TimeSpan ts = new TimeSpan(longTimeStamp); + return dd.Add(ts); + } + + /// + /// 字符串转Guid + /// + /// + /// + + public static Guid? ToGuid(this string guid) + { + try + { + return new Guid(guid); + } + catch (Exception) + { + + throw; + } + } + + #endregion + + #region 数字转字符串前面补零 + public static string IntToString(this int parm, int bit, bool fore = true) + { + int max = (int)(Math.Pow(10, bit) - 1); + if (parm >= 0) + { + if (parm > max) + { + throw new Exception("越界,无法转换"); + } + + } + else + { + if (parm < -max) + { + throw new Exception("越界,无法转换"); + } + + } + + if (fore) + { + return parm.ToString().PadLeft(bit, '0'); + } + else + { + return parm.ToString().PadRight(bit, '0'); + } + } + #endregion + } +}