using System; using System.Drawing; using System.Globalization; using System.IO; using System.Text; namespace KGIS.Plugin.LayerProperty.Utils { public class ConvertUtil { private static UnicodeEncoding ue; static ConvertUtil() { ConvertUtil.ue = new UnicodeEncoding(); } public static int ChangeToInt(bool blnValue) { if (blnValue) { return 1; } return 0; } public static bool ChangeToBool(int pValue) { return pValue >= 1; } public static string Quote(string str) { return "'" + str + "'"; } public static byte[] ConvertStringToByteArray(string s) { return ConvertUtil.ue.GetBytes(s); } public static string Encode(string s) { byte[] array = ConvertUtil.ConvertStringToByteArray(s); return Convert.ToBase64String(array, 0, array.Length); } public static string Decode(string s) { byte[] bytes = Convert.FromBase64String(s); return ConvertUtil.ue.GetString(bytes); } public static void OutputByteArray(ref byte[] b) { for (int i = 0; i < b.Length; i++) { Console.Write((int)b[i]); Console.Write(","); } } public static byte[] ReadByteArrayFromFile(string filePath) { if (!File.Exists(filePath)) { return new byte[0]; } FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); BinaryReader binaryReader = new BinaryReader(fileStream); byte[] result = binaryReader.ReadBytes((int)fileStream.Length); binaryReader.Close(); fileStream.Close(); return result; } public static object ReadObjectFromFile(string filePath) { if (!File.Exists(filePath)) { return new object(); } FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); BinaryReader binaryReader = new BinaryReader(fileStream); object result = binaryReader.ReadBytes((int)fileStream.Length); binaryReader.Close(); fileStream.Close(); return result; } public static double Squre(double input) { return input * input; } public static NumberFormatInfo GetNumFormatInfo(int bits) { NumberFormatInfo numberFormat = new CultureInfo("en-US", false).NumberFormat; numberFormat.NumberDecimalDigits = bits; numberFormat.CurrencyGroupSeparator = ""; numberFormat.NumberGroupSeparator = ""; return numberFormat; } public static int GetStringDBLength(string s) { if (s == null) { return 0; } if (s.Length == 0) { return 0; } byte[] bytes = ConvertUtil.ue.GetBytes(s); int num = 0; for (int i = 1; i < bytes.Length; i += 2) { if (bytes[i] == 0) { num++; } else { num += 2; } } return num; } public static string CutString2DBLength(string s, int len) { if (s == null) { return null; } if (len <= 0) { return ""; } if (s.Length == 0) { return ""; } byte[] bytes = ConvertUtil.ue.GetBytes(s); int num = 0; int i; for (i = 1; i < bytes.Length; i += 2) { if (bytes[i] == 0) { num++; } else { num += 2; } if (num >= len) { break; } } if (num > len) { i -= 2; } if (i <= 0) { return ""; } if (len >= num) { return s; } return ConvertUtil.ue.GetString(bytes, 0, i + 1); } public static string RemoveStringByMark(string s, string mark) { int num = s.LastIndexOf(mark); if (num < 0) { return s; } return s.Substring(num + 1); } public static string SetDecimalPrecision(string s, int bits) { int num = s.IndexOf("."); if (num < 0) { return s; } return Math.Round(Convert.ToDouble(s), bits).ToString(); } public static Image ByteToImage(byte[] blob) { if (blob != null) { MemoryStream memoryStream = new MemoryStream(blob); Image result = Image.FromStream(memoryStream, true); memoryStream.Close(); return result; } return null; } } }