using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace HuizhongLibrary { public class DataConvert { public static DateTime? ToDateTime(object val) { string value=Convert.ToString(val); if (value == null || value == "") return null; DateTime? dt = null; try { dt = Convert.ToDateTime(value); } catch { return null; } return dt; } public static Int32? ToInt32(object val) { string value = Convert.ToString(val); if (value == null || value == "") return null; Int32? dt = null; try { dt = Convert.ToInt32(value); } catch { return null; } return dt; } public static Decimal? ToDecimal(object val) { string value = Convert.ToString(val); if (value == null || value == "") return null; Decimal? dt = null; try { dt = Convert.ToDecimal(value); } catch { return null; } return dt; } public static Double? ToDouble(object val) { string value = Convert.ToString(val); if (value == null || value == "") return null; Double? dt = null; try { dt = Convert.ToDouble(value); } catch { return null; } return dt; } public static Single? ToSingle(object val) { string value = Convert.ToString(val); if (value == null || value == "") return null; Single? dt = null; try { dt = Convert.ToSingle(value); } catch { return null; } return dt; } public static Int16? ToInt16(object val) { string value = Convert.ToString(val); if (value == null || value == "") return null; Int16? dt = null; try { dt = Convert.ToInt16(value); } catch { return null; } return dt; } public static String ToString(object val) { string value = Convert.ToString(val); if (value == null || value == "") return ""; return value.Trim(); } #region 过滤网页标签 public static String CleanHTMLTag(string htmlStream) { if (!string.IsNullOrEmpty(htmlStream)) { /* * 最好把所有的特殊HTML标记都找出来,然后把与其相对应的Unicode字符一起影射到Hash表内,最后一起都替换掉 */ //先单独测试,成功后,再把所有模式合并 //注:这两个必须单独处理 //去掉嵌套了HTML标记的JavaScript:() //去掉css标记:() //去掉css标记:\\..*\\{[\\s\\S]*\\} htmlStream = Regex.Replace(htmlStream, "()|()", " ", RegexOptions.IgnoreCase); //htmlStream = RemoveTag(htmlStream, "script"); //htmlStream = RemoveTag(htmlStream, "style"); //去掉普通HTML标记:<[^>]+> //替换空格: |&|­| |­ htmlStream = Regex.Replace(htmlStream, "<[^>]+>| |&|­| |­|•|<|>", " ", RegexOptions.IgnoreCase); //htmlStream = RemoveTag(htmlStream); //替换左尖括号 //htmlStream = Regex.Replace(htmlStream, "<", "<"); //替换右尖括号 //htmlStream = Regex.Replace(htmlStream, ">", ">"); //替换空行 //htmlStream = Regex.Replace(htmlStream, "[\n|\r|\t]", " ");//[\n|\r][\t*| *]*[\n|\r] htmlStream = Regex.Replace(htmlStream, "(\r\n[\r|\n|\t| ]*\r\n)|(\n[\r|\n|\t| ]*\n)", "\r\n"); htmlStream = Regex.Replace(htmlStream, "[\t| ]{1,}", " "); } return htmlStream.Trim(); } #endregion #region 过滤网页代码 public static String ToCodeString(string value) { if (value == null || value == "") return ""; Dictionary where = new Dictionary(); where.Add(@"

", ""); where.Add(@"

", ""); where.Add(@"

", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"<.xml:namespace .+?>", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"\r", ""); where.Add(@"\n", ""); where.Add(@"\t", ""); where.Add(@" ", " "); where.Add(@"<", "<"); where.Add(@">", ">"); foreach (string s in where.Keys) { value = Regex.Replace(value, s, where[s], RegexOptions.IgnoreCase); } return value; } #endregion /// /// 转换空值 /// /// 校验值 /// 如果是空值,那么用这个值替换 /// public static object IsNull(object value, object defaultValue) { if (value != null && value != DBNull.Value) { return value; } else { return defaultValue; } } public static string Format(object value, TypeCode type, string format) { if (value == null || value.ToString() == "" || value == DBNull.Value) return ""; if (format == null || format == "") return value.ToString(); switch (type) { case TypeCode.DateTime: return Convert.ToDateTime(value).ToString(format); case TypeCode.Decimal: return Convert.ToDecimal(value).ToString(format); case TypeCode.Double: return Convert.ToDouble(value).ToString(format); case TypeCode.Single: return Convert.ToSingle(value).ToString(format); default: return ""; } } public static string Format(object value, string format) { if (value == null || value.ToString() == "" || value == DBNull.Value) return ""; if (format == null || format == "") return value.ToString(); try { return Convert.ToDecimal(value).ToString(format); } catch { } try { return Convert.ToDateTime(value).ToString(format); } catch { } return value.ToString(); } /// /// 十六进制转换到十进制 /// /// /// public static string Hex2Ten(string hex) { int ten = 0; for (int i = 0, j = hex.Length - 1; i < hex.Length; i++) { ten += HexChar2Value(hex.Substring(i, 1)) * ((int)Math.Pow(16, j)); j--; } return ten.ToString(); } public static int HexChar2Value(string hexChar) { switch (hexChar) { case "0": case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": return Convert.ToInt32(hexChar); case "a": case "A": return 10; case "b": case "B": return 11; case "c": case "C": return 12; case "d": case "D": return 13; case "e": case "E": return 14; case "f": case "F": return 15; default: return 0; } } /// /// 从十进制转换到十六进制 /// /// /// public static string Ten2Hex(string ten) { ulong tenValue = Convert.ToUInt64(ten); ulong divValue, resValue; string hex = ""; do { //divValue = (ulong)Math.Floor(tenValue / 16); divValue = (ulong)Math.Floor((decimal)(tenValue / 16)); resValue = tenValue % 16; hex = tenValue2Char(resValue) + hex; tenValue = divValue; } while (tenValue >= 16); if (tenValue != 0) hex = tenValue2Char(tenValue) + hex; return hex; } public static string tenValue2Char(ulong ten) { switch (ten) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: return ten.ToString(); case 10: return "A"; case 11: return "B"; case 12: return "C"; case 13: return "D"; case 14: return "E"; case 15: return "F"; default: return ""; } } } }