using System;
using System.Text;
namespace Admin.Core
{
///
/// 数据类型转换
///
public static class UtilConvert
{
public static int ToInt(this object thisValue)
{
int reval = 0;
if (thisValue == null) return 0;
if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return reval;
}
public static int ToInt(this object thisValue, int errorValue)
{
int reval = 0;
if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
public static long ToLong(this object s)
{
if (s == null || s == DBNull.Value)
return 0L;
long.TryParse(s.ToString(), out long result);
return result;
}
public static double ToMoney(this object thisValue)
{
double reval = 0;
if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return 0;
}
public static double ToMoney(this object thisValue, double errorValue)
{
double reval = 0;
if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
public static string ToString(this object thisValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
return "";
}
public static string ToString(this object thisValue, string errorValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
return errorValue;
}
public static decimal ToDecimal(this object thisValue)
{
Decimal reval = 0;
if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return 0;
}
public static decimal ToDecimal(this object thisValue, decimal errorValue)
{
Decimal reval = 0;
if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
public static DateTime ToDate(this object thisValue)
{
DateTime reval = DateTime.MinValue;
if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
{
reval = Convert.ToDateTime(thisValue);
}
return reval;
}
public static DateTime ToDate(this object thisValue, DateTime errorValue)
{
DateTime reval = DateTime.MinValue;
if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
public static bool ToBool(this object thisValue)
{
bool reval = false;
if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return reval;
}
public static byte ToByte(this object s)
{
if (s == null || s == DBNull.Value)
return 0;
byte.TryParse(s.ToString(), out byte result);
return result;
}
#region ==字节转换==
///
/// 转换为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[] HexToBytes(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;
}
///
/// 转换为Base64
///
///
///
public static string ToBase64(this byte[] bytes)
{
if (bytes == null)
return null;
return Convert.ToBase64String(bytes);
}
#endregion
}
}