namespace Common { using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text.RegularExpressions; using System.Linq; 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; } //字符串转数组 public static object[] stringToArray(string str) { object[] array = str.Split(','); return array; } //数组转字符串 public static string arrayToString(object[] array) { if (array == null) return ""; string str = string.Join(",", array); return str; } //数组转换List:string[] str ={ "str","string","abc"}转 List public static List arrayToList(object[] array) { List list = new List(array); return list; } //List转换数组:List转到string[] public static object[] listToArray(List list) { object[] array = list.ToArray(); return array; } //字符串转List public static List stringToList(string str) { if (string.IsNullOrEmpty(str)) return null; List list = new List(); //字符串转数组,再数组合并 list.AddRange(str.Split(',')); return list; } //数组去重,移除数组中重复数据 public static string[] DelRepeatData(string[] array) { return array.GroupBy(p => p).Select(p => p.Key).ToArray(); } //检查数组格式字符串中是否包含某元素 public static bool checkStrForArrayStr(object str, string arrayStr ) { return stringToArray(arrayStr).Contains(str); } //检查数组中是否包含某元素 public static bool checkStrForArray(object str, object[] array) { return array.Contains(str); } //根据数组字符串转换字典 public Dictionary arrayStrToDict(object userId, string arrayStr) { Dictionary dictionary = new Dictionary(); if (string.IsNullOrEmpty(arrayStr)) return dictionary; List list = DataConverter.stringToList(arrayStr); for (int i = 0; i < list.Count; i++) { dictionary[userId.ToString()] = list[i]; } return dictionary; } } }