namespace Common { using System; using System.Runtime.InteropServices; using System.Text.RegularExpressions; public class DataConverter { protected DataConverter() { } public static DateTime BigIntToDateTime(long inputLong) { DateTime date = DateTime.Now.Date; DateTime.TryParse("1970-01-01", out date); double num = ((double)inputLong) / 86400.0; return date.AddDays(num); } public static string getDateDotSplit(DateTime? currentTime, string split) { string str = "待定"; if (currentTime.HasValue) { str = string.Concat(new object[] { currentTime.Value.Year, split, currentTime.Value.Month, split, currentTime.Value.Day }); } return str; } public static string IntToStringIP(int ip) { StructIp ip2 = new StructIp { _IntIP = ip }; return (ip2._IP.ip1.ToString() + "." + ip2._IP.ip2.ToString() + "." + ip2._IP.ip3.ToString() + "." + ip2._IP.ip4.ToString()); } public static int SafeInt32(object objNum) { if (objNum != null) { string strNumber = objNum.ToString(); if (DataValidate.IsNumber(strNumber)) { if (strNumber.ToString().Length > 9) { return 0x7fffffff; } return int.Parse(strNumber); } } return 0; } public static bool StrToBool(string strValue) { if (!string.IsNullOrEmpty(strValue)) { strValue = strValue.Trim(); return (((string.Compare(strValue, "true", true) == 0) || (string.Compare(strValue, "yes", true) == 0)) || (string.Compare(strValue, "1", true) == 0)); } return false; } public static DateTime StrToDateTime(object strValue, DateTime defValue) { DateTime time; if ((strValue == null) || (strValue.ToString().Length > 20)) { return defValue; } if (!DateTime.TryParse(strValue.ToString(), out time)) { time = defValue; } return time; } public static decimal StrToDecimal(object strValue) { if (!(Convert.IsDBNull(strValue) || object.Equals(strValue, null))) { return StrToDecimal(strValue.ToString()); } return 0M; } public static decimal StrToDecimal(string strValue) { decimal num; decimal.TryParse(strValue, out num); return num; } public static decimal StrToDecimal(string input, decimal defaultValue) { decimal num; if (decimal.TryParse(input, out num)) { return num; } return defaultValue; } public static double StrToDouble(object strValue) { if (!(Convert.IsDBNull(strValue) || object.Equals(strValue, null))) { return StrToDouble(strValue.ToString()); } return 0.0; } public static double StrToDouble(string strValue) { double num; double.TryParse(strValue, out num); return num; } public static int StrToInt(object strValue) { if ((((strValue != null) && (strValue.ToString() != string.Empty)) && (strValue.ToString().Length <= 10)) && Regex.IsMatch(strValue.ToString(), "^[0-9]+$")) { return Convert.ToInt32(strValue.ToString()); } return -1; } public static int StrToInt(object strValue, int defValue) { if (((strValue != null) && (strValue.ToString() != string.Empty)) && Regex.IsMatch(strValue.ToString(), "^[0-9]+$")) { return Convert.ToInt32(strValue.ToString()); } return defValue; } public static long StrToInt64(string strValue, long defValue) { long result = 0L; if (long.TryParse(strValue, out result)) { return result; } return defValue; } public static int StrToPageInt(object strValue, int defValue) { if (((strValue != null) && (strValue.ToString() != string.Empty)) && Regex.IsMatch(strValue.ToString(), "^[0-9]+$")) { int num = Convert.ToInt32(strValue.ToString()); if (num < 1) { num = 1; } return num; } return defValue; } [StructLayout(LayoutKind.Sequential)] private struct IPAdress { public byte ip1; public byte ip2; public byte ip3; public byte ip4; } [StructLayout(LayoutKind.Explicit)] private struct StructIp { [FieldOffset(0)] public int _IntIP; [FieldOffset(0)] public DataConverter.IPAdress _IP; } //获取随机码 public static string getRandom(int strLength) { string random = ""; if (strLength <= 0) { strLength = 6; //默认6位长度 } //random = Guid.NewGuid().ToString().Replace("-", "").Substring(0, strLength); random = Guid.NewGuid().ToString(); random = random.Replace("-", ""); random = random.Substring(0, strLength); return random; } } }