csharp/17MKH/Mkh/src/01_Utils/Utils/Extensions/CommonExtensions.cs

CommonExtensions.cs
using System;
using System.Text;

// ReSharper disable once CheckNamespace
namespace Mkh;

/// 
/// 通用扩展方法
/// 
public static clast CommonExtensions
{
    #region ==数据转换扩展==

    /// 
    /// 转换成Byte
    /// 
    /// 输入字符串
    /// 
    public static byte ToByte(this string s)
    {
        if (s.IsNull())
            return 0;

        byte.TryParse(s, out byte result);
        return result;
    }

    /// 
    /// 转换成Char
    /// 
    /// 
    /// 
    public static char ToChar(this string s)
    {
        if (s.IsNull())
            return default;

        char.TryParse(s, out char result);
        return result;
    }

    /// 
    /// 转换成Char
    /// 
    /// 
    /// 
    public static char ToChar(this int s)
    {
        return (char)s;
    }

    /// 
    /// 转换成short/Int16
    /// 
    /// 
    /// 
    public static short ToShort(this string s)
    {
        if (s.IsNull())
            return 0;

        short.TryParse(s, 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 is bool b)
            return b ? 1 : 0;

        if (int.TryParse(s.ToString(), out int result))
            return result;

        if (s.GetType().IsEnum)
        {
            return (int)s;
        }

        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;
    }

    /// 
    /// 字符串转Guid
    /// 
    /// 
    /// 
    public static Guid ToGuid(this string s)
    {
        if (s.NotNull() && Guid.TryParse(s, out Guid val))
            return val;

        return Guid.Empty;
    }

    /// 
    /// 泛型转换,转换失败会抛出异常
    /// 
    /// 
    /// 
    /// 
    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 ==字节转换==

    /// 
    /// 转为十六进制
    /// 
    /// 
    /// 
    /// 
    public static string ToHex(this string val, bool lowerCase = true)
    {
        if (val.IsNull())
            return null;

        var bytes = Encoding.UTF8.GetBytes(val);
        return bytes.ToHex(lowerCase);
    }

    /// 
    /// 转换为16进制
    /// 
    /// 
    /// 是否小写
    /// 
    public static string ToHex(this byte[] bytes, bool lowerCase = true)
    {
        if (bytes == null)
            return null;

        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[] Hex2Bytes(this string s)
    {
        if (s.IsNull())
            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;
    }

    /// 
    /// 16进制转字符串
    /// 
    /// 
    /// 
    public static string Hex2String(this string val)
    {
        if (val.IsNull())
            return null;

        var bytes = val.Hex2Bytes();
        return Encoding.UTF8.GetString(bytes);
    }

    #endregion
}