using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace SKMC.Api.Common
{
///
/// 字符串等数值变换工具类
///
public class CommonUtil
{
///
/// 创建一个基于Guid的long类型Id
///
///
public static long LongId()
{
byte[] guidBytes = Guid.NewGuid().ToByteArray();
return BitConverter.ToInt64(guidBytes, 0) & long.MaxValue;
}
///
/// 生成一个基于Guid连续字符的字符串Id
///
///
public static string StringId()
{
return Guid.NewGuid().ToString("N");
}
///
/// String类型変換
///
/// 目标对象
///
public static string GetStringValue(object value)
{
return GetStringValue(value, string.Empty);
}
///
/// String类型変換
///
/// 目标对象
/// 默认值
///
public static string GetStringValue(object value, string defaultValue)
{
if (Convert.IsDBNull(value))
{
return defaultValue;
}
else if (value == null)
{
return defaultValue;
}
else
{
return value.ToString();
}
}
///
/// Bool类型変換
///
/// 目标对象
///
public static bool GetBoolValue(string value)
{
bool bValue;
Boolean.TryParse(value, out bValue);
return bValue;
}
///
/// Int类型変換
///
/// 目标对象
///
public static int GetIntValue(object value)
{
return CommonUtil.GetIntValue(value, 0);
}
///
/// Int类型変換
///
/// 目标对象
/// 默认值
///
public static int GetIntValue(object value, int defaultValue)
{
if (Convert.IsDBNull(value))
{
return defaultValue;
}
else if (value == null)
{
return defaultValue;
}
int result;
if (int.TryParse(value.ToString(), out result) == true)
{
return result;
}
else
{
return defaultValue;
}
}
///
/// Float类型変換
///
/// 目标对象
///
public static float GetFloatValue(object value)
{
return GetFloatValue(value, 0);
}
///
/// Float类型変換
///
/// 目标对象
///
public static float GetNotNullFloatValue(Nullable data)
{
if (data != null)
{
return GetFloatValue(data.Value, 0);
}
else
{
return 0;
}
}
///
/// Float类型変換
///
/// 目标对象
/// 默认值
///
public static float GetFloatValue(object value, float defaultValue)
{
if (Convert.IsDBNull(value))
{
return defaultValue;
}
else if (value == null)
{
return defaultValue;
}
float result;
if (float.TryParse(value.ToString(), out result) == true)
{
return result;
}
else
{
return defaultValue;
}
}
///
/// 比较两个字符串类型的值是否相同
///
/// 字符串1
/// 字符串2
///
public static bool StringCompare(string value1, string value2)
{
bool result;
if ((value1 == null) && (value2 == null))
{
result = true;
}
if ((value1 == null) || (value2 == null))
{
result = false;
}
else
{
result = value1.Equals(value2);
}
return result;
}
///
/// 确认指定的字符串是否为空
///
/// 対象文字列
///
public static bool IsEmptyString(string value)
{
if (value == null)
{
return true;
}
if (StringCompare(value.Trim(), string.Empty) == true)
{
return true;
}
return false;
}
public static bool IsEmptyList(IEnumerable source)
{
if (source == null) return true;
return !source.Any();
}
///
/// 小数标记验证
///
/// 验证对象
/// 整数位数
/// 小数位数
///
public static bool IsDecimalNumber(object target, int intDigit, int decDigit)
{
bool rlt = false;
if (intDigit > 0 && decDigit > 0 && IsDecimal(GetStringValue(target)))
{
StringBuilder regularExp = new StringBuilder();
regularExp.Append("^\\-?[0-9]{1,");
regularExp.Append(intDigit);
regularExp.Append("}(\\.[0-9]{1,");
regularExp.Append(decDigit);
regularExp.Append("})?$");
if (Regex.IsMatch(target.ToString(), regularExp.ToString()))
{
rlt = true;
}
}
return rlt;
}
///
/// 是否为Decimal
///
///
///
public static bool IsDecimal(string target)
{
bool result = false;
decimal decNum;
if (Decimal.TryParse(target, out decNum))
{
result = true;
}
else
{
result = false;
}
return result;
}
///
/// double类型変換
///
/// 目标对象
///
public static double GetDoubleValue(object value)
{
return GetDoubleValue(value, 0);
}
///
/// double类型変換
///
/// 目标对象
/// 默认值
///
public static double GetDoubleValue(object value, double defaultValue)
{
if (Convert.IsDBNull(value))
{
return defaultValue;
}
else if (value == null)
{
return defaultValue;
}
double result;
if (double.TryParse(value.ToString(), out result) == true)
{
return result;
}
else
{
return defaultValue;
}
}
///
/// 舍入小于或等于默认小数
///
/// 対象数值
/// 显示小数位数格式({0:##0.000})
/// 結果
public static float CutDecimalValue(float value, string format)
{
string strNewWithoutDec = string.Format(format, value);
return GetFloatValue(strNewWithoutDec);
}
///
/// 将数字转换为Byte数组(翻转)
///
/// 数值数据
/// byte数组
public static byte[] GetIntReversedByteValue(int data, int len)
{
byte[] inValue = BitConverter.GetBytes(data);
byte[] retValue = new byte[len];
for (int i = 0; i < len; i++)
{
retValue[i] = (i < inValue.Length) ? inValue[i] : (byte)0;
}
// 翻转
Array.Reverse(retValue);
return retValue;
}
///
/// 将数字转换为Byte数组
///
/// 数值数据
/// byte数组
public static byte[] GetIntByteValue(int data, int len)
{
byte[] inValue = BitConverter.GetBytes(data);
byte[] retValue = new byte[len];
for (int i = 0; i < len; i++)
{
retValue[i] = (i < inValue.Length) ? inValue[i] : (byte)0;
}
return retValue;
}
public static string PasswordEncryption(string pwd)
{
SHA1 sha1 = SHA1.Create();
byte[] originalPwd = Encoding.UTF8.GetBytes(pwd);
byte[] encryPwd = sha1.ComputeHash(originalPwd);
return string.Join("", encryPwd.Select(b => string.Format("{0:x2}", b)).ToArray()).ToUpper();
}
}
}