C#实用函数大全

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using Microsoft.Win32; //对注册表操作  
  5. using System.Collections; //使用Arraylist  
  6. using System.Security.Cryptography;//加密解密  
  7. using System.IO;    //文件操作  
  8. using System.Runtime.InteropServices;//调用DLL DllImport  
  9. using System.Management;  //获取硬件信息  
  10. using System.Net;       //获取IP地址是用到  
  11. using System.Drawing;   //image   
  12. using System.Net.NetworkInformation;    //ping 用到  
  13. using System.Text.RegularExpressions;   //正则  
  14. using System.Data;  
  15. using System.Data.SqlClient;  
  16. using Microsoft.VisualBasic;   //简体转繁体时用到  
  17. using System.Web;       //html UrlEncode  
  18.  
  19. #region 注册表操作  
  20. public class GF_RegReadWrite  
  21. {  
  22.   
  23.     /// <summary>  
  24.     /// 读取路径为keypath,键名为keyname的注册表键值,缺省返回def  
  25.     /// </summary>  
  26.     /// <param name="rootkey"></param>  
  27.     /// <param name="keypath">路径</param>  
  28.     /// <param name="keyname">键名</param>  
  29.     /// <param name="rtn">默认为null</param>  
  30.     /// <returns></returns>          
  31.     static public bool GetRegVal(RegistryKey rootkey, string keypath, string keyname, out string rtn)  
  32.     {  
  33.         rtn = "";  
  34.         try  
  35.         {  
  36.             RegistryKey key = rootkey.OpenSubKey(keypath);  
  37.             rtn = key.GetValue(keyname).ToString();  
  38.             key.Close();  
  39.             return true;  
  40.         }  
  41.         catch  
  42.         {  
  43.             return false;  
  44.         }  
  45.     }  
  46.   
  47.     /// <summary>  
  48.     /// 设置路径为keypath,键名为keyname的注册表键值为keyval  
  49.     /// </summary>  
  50.     /// <param name="rootkey"></param>  
  51.     /// <param name="keypath"></param>  
  52.     /// <param name="keyname"></param>  
  53.     /// <param name="keyval"></param>  
  54.     /// <returns></returns>  
  55.     static public bool SetRegVal(RegistryKey rootkey, string keypath, string keyname, string keyval)  
  56.     {  
  57.         try  
  58.         {  
  59.             RegistryKey key = rootkey.OpenSubKey(keypath, true);  
  60.             if (key == null)  
  61.                 key = rootkey.CreateSubKey(keypath);  
  62.             key.SetValue(keyname, (object)keyval);  
  63.             key.Close();  
  64.             return true;  
  65.         }  
  66.         catch  
  67.         {  
  68.             return false;  
  69.         }  
  70.     }  
  71.   
  72.     /// 创建路径为keypath的键  
  73.     private RegistryKey CreateRegKey(RegistryKey rootkey, string keypath)  
  74.     {  
  75.         try  
  76.         {  
  77.             return rootkey.CreateSubKey(keypath);  
  78.         }  
  79.         catch  
  80.         {  
  81.             return null;  
  82.         }  
  83.     }  
  84.     /// 删除路径为keypath的子项  
  85.     private bool DelRegSubKey(RegistryKey rootkey, string keypath)  
  86.     {  
  87.         try  
  88.         {  
  89.             rootkey.DeleteSubKey(keypath);  
  90.             return true;  
  91.         }  
  92.         catch  
  93.         {  
  94.             return false;  
  95.         }  
  96.     }  
  97.     /// 删除路径为keypath的子项及其附属子项  
  98.     private bool DelRegSubKeyTree(RegistryKey rootkey, string keypath)  
  99.     {  
  100.         try  
  101.         {  
  102.             rootkey.DeleteSubKeyTree(keypath);  
  103.             return true;  
  104.         }  
  105.         catch  
  106.         {  
  107.             return false;  
  108.         }  
  109.     }  
  110.     /// 删除路径为keypath下键名为keyname的键值  
  111.     private bool DelRegKeyVal(RegistryKey rootkey, string keypath, string keyname)  
  112.     {  
  113.         try  
  114.         {  
  115.             RegistryKey key = rootkey.OpenSubKey(keypath, true);  
  116.             key.DeleteValue(keyname);  
  117.             return true;  
  118.         }  
  119.         catch  
  120.         {  
  121.             return false;  
  122.         }  
  123.     }  
  124. }  
  125. #endregion  
  126. #region 类型转换  
  127. public class GF_Convert  
  128. {  
  129.     /// <summary>  
  130.     /// 字符串 转换 char数组  
  131.     /// </summary>  
  132.     /// <param name="in_str"></param>  
  133.     /// <param name="in_len"></param>  
  134.     /// <returns></returns>  
  135.     public static char[] string2chararray(string in_str, int in_len)  
  136.     {  
  137.         char[] ch = new char[in_len];  
  138.         in_str.ToCharArray().CopyTo(ch, 0);  
  139.         return ch;  
  140.     }  
  141.   
  142.     /// <summary>  
  143.     /// char数组 转换 字符串  
  144.     /// </summary>  
  145.     /// <param name="in_str"></param>  
  146.     /// <returns></returns>          
  147.     public static string chararray2string(char[] in_str)  
  148.     {  
  149.         string out_str;  
  150.         out_str = new string(in_str);  
  151.         int i = out_str.IndexOf('\0', 0);  
  152.         if (i == -1)  
  153.             i = 16;  
  154.         return out_str.Substring(0, i);  
  155.     }  
  156.   
  157.     /// <summary>  
  158.     /// byte数组 转换 字符串  
  159.     /// </summary>  
  160.     /// <param name="in_str"></param>  
  161.     /// <returns></returns>  
  162.     public static string bytearray2string(byte[] in_str)  
  163.     {  
  164.         string out_str;  
  165.         out_str = System.Text.Encoding.Default.GetString(in_str);  
  166.         return out_str.Substring(0, out_str.IndexOf('\0', 0));  
  167.   
  168.     }  
  169.   
  170.     /// <summary>  
  171.     /// 字符串 转换 byte数组  注意转换出来会使原来的bytearray长度变短  
  172.     /// </summary>  
  173.     /// <param name="in_str"></param>  
  174.     /// <returns></returns>  
  175.     public static byte[] string2bytearray(string in_str)  
  176.     {  
  177.         return System.Text.Encoding.Default.GetBytes(in_str);  
  178.     }  
  179.   
  180.     /// <summary>  
  181.     /// 字符串 转换 byte数组  长度为传如的长度  
  182.     /// </summary>  
  183.     /// <param name="in_str">传入字符串</param>  
  184.     /// <param name="iLen">目标字节数组长度</param>  
  185.     /// <returns></returns>  
  186.     public static byte[] string2bytearray(string in_str, int iLen)  
  187.     {  
  188.         byte[] bytes = new byte[iLen];  
  189.         byte[] bsources = System.Text.Encoding.Default.GetBytes(in_str);  
  190.         Array.Copy(bsources, bytes, bsources.Length);  
  191.   
  192.   
  193.         return bytes;  
  194.     }  
  195.   
  196.     /// <summary>  
  197.     /// 将字符串编码为Base64字符串  
  198.     /// </summary>  
  199.     /// <param name="str"></param>  
  200.     /// <returns></returns>  
  201.     public static string Base64Encode(string str)  
  202.     {  
  203.         byte[] barray;  
  204.         barray = Encoding.Default.GetBytes(str);  
  205.         return Convert.ToBase64String(barray);  
  206.     }  
  207.   
  208.     /// <summary>  
  209.     /// 将Base64字符串解码为普通字符串  
  210.     /// </summary>  
  211.     /// <param name="str"></param>  
  212.     /// <returns></returns>  
  213.     public static string Base64Decode(string str)  
  214.     {  
  215.         byte[] barray;  
  216.         try  
  217.         {  
  218.             barray = Convert.FromBase64String(str);  
  219.             return Encoding.Default.GetString(barray);  
  220.         }  
  221.         catch  
  222.         {  
  223.             return str;  
  224.         }  
  225.     }  
  226.   
  227.     /// <summary>  
  228.     /// 图片 转换 byte数组  
  229.     /// </summary>  
  230.     /// <param name="pic"></param>  
  231.     /// <param name="fmt"></param>  
  232.     /// <returns></returns>  
  233.     public static byte[] image_Image2Byte(Image pic, System.Drawing.Imaging.ImageFormat fmt)  
  234.     {  
  235.         MemoryStream mem = new MemoryStream();  
  236.         pic.Save(mem, fmt);  
  237.         mem.Flush();  
  238.         return mem.ToArray();  
  239.     }  
  240.     /// <summary>  
  241.     /// byte数组 转换 图片  
  242.     /// </summary>  
  243.     /// <param name="bytes"></param>  
  244.     /// <returns></returns>  
  245.     public static Image image_Byte2Image(byte[] bytes)  
  246.     {  
  247.         MemoryStream mem = new MemoryStream(bytes, true);  
  248.         mem.Read(bytes, 0, bytes.Length);  
  249.         mem.Flush();  
  250.         Image aa = Image.FromStream(mem);  
  251.         return aa;  
  252.     }  
  253.   
  254.     /// <summary>  
  255.     /// ip 转换 长整形  
  256.     /// </summary>  
  257.     /// <param name="strIP"></param>  
  258.     /// <returns></returns>  
  259.     public static long IP2Long(string strIP)  
  260.     {  
  261.   
  262.         long[] ip = new long[4];  
  263.   
  264.         string[] s = strIP.Split('.');  
  265.         ip[0] = long.Parse(s[0]);  
  266.         ip[1] = long.Parse(s[1]);  
  267.         ip[2] = long.Parse(s[2]);  
  268.         ip[3] = long.Parse(s[3]);  
  269.   
  270.         return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];  
  271.     }  
  272.   
  273.     /// <summary>  
  274.     /// 长整形 转换 IP  
  275.     /// </summary>  
  276.     /// <param name="longIP"></param>  
  277.     /// <returns></returns>  
  278.     public static string Long2IP(long longIP)  
  279.     {  
  280.   
  281.   
  282.         StringBuilder sb = new StringBuilder("");  
  283.         sb.Append(longIP >> 24);  
  284.         sb.Append(".");  
  285.   
  286.         //将高8位置0,然后右移16为  
  287.   
  288.   
  289.         sb.Append((longIP & 0x00FFFFFF) >> 16);  
  290.         sb.Append(".");  
  291.   
  292.   
  293.         sb.Append((longIP & 0x0000FFFF) >> 8);  
  294.         sb.Append(".");  
  295.   
  296.         sb.Append((longIP & 0x000000FF));  
  297.   
  298.   
  299.         return sb.ToString();  
  300.     }  
  301.   
  302.     /// <summary>  
  303.     /// 将8位日期型整型数据转换为日期字符串数据  
  304.     /// </summary>  
  305.     /// <param name="date">整型日期</param>  
  306.     /// <param name="chnType">是否以中文年月日输出</param>  
  307.     /// <returns></returns>  
  308.     public static string FormatDate(int date, bool chnType)  
  309.     {  
  310.         string dateStr = date.ToString();  
  311.   
  312.         if (date <= 0 || dateStr.Length != 8)  
  313.             return dateStr;  
  314.   
  315.         if (chnType)  
  316.             return dateStr.Substring(0, 4) + "年" + dateStr.Substring(4, 2) + "月" + dateStr.Substring(6) + "日";  
  317.   
  318.         return dateStr.Substring(0, 4) + "-" + dateStr.Substring(4, 2) + "-" + dateStr.Substring(6);  
  319.     }  
  320.   
  321.   
  322.     /// <summary>  
  323.     /// string型转换为bool型  
  324.     /// </summary>  
  325.     /// <param name="strValue">要转换的字符串</param>  
  326.     /// <param name="defValue">缺省值</param>  
  327.     /// <returns>转换后的bool类型结果</returns>  
  328.     public static bool StrToBool(object expression, bool defValue)  
  329.     {  
  330.         if (expression != null)  
  331.             return StrToBool(expression, defValue);  
  332.   
  333.         return defValue;  
  334.     }  
  335.   
  336.     /// <summary>  
  337.     /// string型转换为bool型  
  338.     /// </summary>  
  339.     /// <param name="strValue">要转换的字符串</param>  
  340.     /// <param name="defValue">缺省值</param>  
  341.     /// <returns>转换后的bool类型结果</returns>  
  342.     public static bool StrToBool(string expression, bool defValue)  
  343.     {  
  344.         if (expression != null)  
  345.         {  
  346.             if (string.Compare(expression, "true"true) == 0)  
  347.                 return true;  
  348.             else if (string.Compare(expression, "false"true) == 0)  
  349.                 return false;  
  350.         }  
  351.         return defValue;  
  352.     }  
  353.     /// <summary>  
  354.     /// 将对象转换为Int32类型  
  355.     /// </summary>  
  356.     /// <param name="strValue">要转换的字符串</param>  
  357.     /// <param name="defValue">缺省值</param>  
  358.     /// <returns>转换后的int类型结果</returns>  
  359.     public static int ObjectToInt(object expression)  
  360.     {  
  361.         return ObjectToInt(expression, 0);  
  362.     }  
  363.   
  364.     /// <summary>  
  365.     /// 将对象转换为Int32类型  
  366.     /// </summary>  
  367.     /// <param name="strValue">要转换的字符串</param>  
  368.     /// <param name="defValue">缺省值</param>  
  369.     /// <returns>转换后的int类型结果</returns>  
  370.     public static int ObjectToInt(object expression, int defValue)  
  371.     {  
  372.         if (expression != null)  
  373.             return StrToInt(expression.ToString(), defValue);  
  374.   
  375.         return defValue;  
  376.     }  
  377.   
  378.     /// <summary>  
  379.     /// 将对象转换为Int32类型,转换失败返回0  
  380.     /// </summary>  
  381.     /// <param name="str">要转换的字符串</param>  
  382.     /// <returns>转换后的int类型结果</returns>  
  383.     public static int StrToInt(string str)  
  384.     {  
  385.         return StrToInt(str, 0);  
  386.     }  
  387.   
  388.     /// <summary>  
  389.     /// 将对象转换为Int32类型  
  390.     /// </summary>  
  391.     /// <param name="str">要转换的字符串</param>  
  392.     /// <param name="defValue">缺省值</param>  
  393.     /// <returns>转换后的int类型结果</returns>  
  394.     public static int StrToInt(string str, int defValue)  
  395.     {  
  396.         if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))  
  397.             return defValue;  
  398.   
  399.         int rv;  
  400.         if (Int32.TryParse(str, out rv))  
  401.             return rv;  
  402.   
  403.         return Convert.ToInt32(StrToFloat(str, defValue));  
  404.     }  
  405.   
  406.     /// <summary>  
  407.     /// string型转换为float型  
  408.     /// </summary>  
  409.     /// <param name="strValue">要转换的字符串</param>  
  410.     /// <param name="defValue">缺省值</param>  
  411.     /// <returns>转换后的int类型结果</returns>  
  412.     public static float StrToFloat(object strValue, float defValue)  
  413.     {  
  414.         if ((strValue == null))  
  415.             return defValue;  
  416.   
  417.         return StrToFloat(strValue.ToString(), defValue);  
  418.     }  
  419.   
  420.     /// <summary>  
  421.     /// string型转换为float型  
  422.     /// </summary>  
  423.     /// <param name="strValue">要转换的字符串</param>  
  424.     /// <param name="defValue">缺省值</param>  
  425.     /// <returns>转换后的int类型结果</returns>  
  426.     public static float ObjectToFloat(object strValue, float defValue)  
  427.     {  
  428.         if ((strValue == null))  
  429.             return defValue;  
  430.   
  431.         return StrToFloat(strValue.ToString(), defValue);  
  432.     }  
  433.   
  434.     /// <summary>  
  435.     /// string型转换为float型  
  436.     /// </summary>  
  437.     /// <param name="strValue">要转换的字符串</param>  
  438.     /// <param name="defValue">缺省值</param>  
  439.     /// <returns>转换后的int类型结果</returns>  
  440.     public static float ObjectToFloat(object strValue)  
  441.     {  
  442.         return ObjectToFloat(strValue.ToString(), 0);  
  443.     }  
  444.   
  445.     /// <summary>  
  446.     /// string型转换为float型  
  447.     /// </summary>  
  448.     /// <param name="strValue">要转换的字符串</param>  
  449.     /// <returns>转换后的int类型结果</returns>  
  450.     public static float StrToFloat(string strValue)  
  451.     {  
  452.         if ((strValue == null))  
  453.             return 0;  
  454.   
  455.         return StrToFloat(strValue.ToString(), 0);  
  456.     }  
  457.   
  458.     /// <summary>  
  459.     /// string型转换为float型  
  460.     /// </summary>  
  461.     /// <param name="strValue">要转换的字符串</param>  
  462.     /// <param name="defValue">缺省值</param>  
  463.     /// <returns>转换后的int类型结果</returns>  
  464.     public static float StrToFloat(string strValue, float defValue)  
  465.     {  
  466.         if ((strValue == null) || (strValue.Length > 10))  
  467.             return defValue;  
  468.   
  469.         float intValue = defValue;  
  470.         if (strValue != null)  
  471.         {  
  472.             bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");  
  473.             if (IsFloat)  
  474.                 float.TryParse(strValue, out intValue);  
  475.         }  
  476.         return intValue;  
  477.     }  
  478.   
  479.     /// <summary>  
  480.     /// 将对象转换为日期时间类型  
  481.     /// </summary>  
  482.     /// <param name="str">要转换的字符串</param>  
  483.     /// <param name="defValue">缺省值</param>  
  484.     /// <returns>转换后的int类型结果</returns>  
  485.     public static DateTime StrToDateTime(string str, DateTime defValue)  
  486.     {  
  487.         if (!string.IsNullOrEmpty(str))  
  488.         {  
  489.             DateTime dateTime;  
  490.             if (DateTime.TryParse(str, out dateTime))  
  491.                 return dateTime;  
  492.         }  
  493.         return defValue;  
  494.     }  
  495.   
  496.     /// <summary>  
  497.     /// 将对象转换为日期时间类型  
  498.     /// </summary>  
  499.     /// <param name="str">要转换的字符串</param>  
  500.     /// <returns>转换后的int类型结果</returns>  
  501.     public static DateTime StrToDateTime(string str)  
  502.     {  
  503.         return StrToDateTime(str, DateTime.Now);  
  504.     }  
  505.   
  506.     /// <summary>  
  507.     /// 将对象转换为日期时间类型  
  508.     /// </summary>  
  509.     /// <param name="obj">要转换的对象</param>  
  510.     /// <returns>转换后的int类型结果</returns>  
  511.     public static DateTime ObjectToDateTime(object obj)  
  512.     {  
  513.         return StrToDateTime(obj.ToString());  
  514.     }  
  515.   
  516.     /// <summary>  
  517.     /// 将对象转换为日期时间类型  
  518.     /// </summary>  
  519.     /// <param name="obj">要转换的对象</param>  
  520.     /// <param name="defValue">缺省值</param>  
  521.     /// <returns>转换后的int类型结果</returns>  
  522.     public static DateTime ObjectToDateTime(object obj, DateTime defValue)  
  523.     {  
  524.         return StrToDateTime(obj.ToString(), defValue);  
  525.     }  
  526.   
  527.     /// <summary>  
  528.     /// 替换回车换行符为html换行符  
  529.     /// </summary>  
  530.     public static string StrFormat(string str)  
  531.     {  
  532.         string str2;  
  533.   
  534.         if (str == null)  
  535.         {  
  536.             str2 = "";  
  537.         }  
  538.         else  
  539.         {  
  540.             str = str.Replace("\r\n""<br />");  
  541.             str = str.Replace("\n""<br />");  
  542.             str2 = str;  
  543.         }  
  544.         return str2;  
  545.     }  
  546.   
  547.     /// <summary>  
  548.     /// 转换为简体中文  
  549.     /// </summary>  
  550.     public static string ToSChinese(string str)  
  551.     {  
  552.         return Strings.StrConv(str, VbStrConv.SimplifiedChinese, 0);  
  553.   
  554.     }  
  555.   
  556.     /// <summary>  
  557.     /// 转换为繁体中文  
  558.     /// </summary>  
  559.     public static string ToTChinese(string str)  
  560.     {  
  561.          
  562.         return Strings.StrConv(str, VbStrConv.TraditionalChinese, 0);  
  563.   
  564.     }  
  565.   
  566.   
  567.     /// <summary>  
  568.     /// 清除字符串数组中的重复项  
  569.     /// </summary>  
  570.     /// <param name="strArray">字符串数组</param>  
  571.     /// <param name="maxElementLength">字符串数组中单个元素的最大长度</param>  
  572.     /// <returns></returns>  
  573.     public static string[] DistinctStringArray(string[] strArray, int maxElementLength)  
  574.     {  
  575.         Hashtable h = new Hashtable();  
  576.   
  577.         foreach (string s in strArray)  
  578.         {  
  579.             string k = s;  
  580.             if (maxElementLength > 0 && k.Length > maxElementLength)  
  581.             {  
  582.                 k = k.Substring(0, maxElementLength);  
  583.             }  
  584.             h[k.Trim()] = s;  
  585.         }  
  586.   
  587.         string[] result = new string[h.Count];  
  588.   
  589.         h.Keys.CopyTo(result, 0);  
  590.   
  591.         return result;  
  592.     }  
  593.   
  594.     /// <summary>  
  595.     /// 清除字符串数组中的重复项  
  596.     /// </summary>  
  597.     /// <param name="strArray">字符串数组</param>  
  598.     /// <returns></returns>  
  599.     public static string[] DistinctStringArray(string[] strArray)  
  600.     {  
  601.         return DistinctStringArray(strArray, 0);  
  602.     }  
  603.   
  604. }  
  605.  
  606. #endregion  
  607. #region 加密解密  
  608. public class GF_Encrypt  
  609. {  
  610.     /// <summary>  
  611.     /// DES加密  
  612.     /// </summary>  
  613.     /// <param name="pToEncrypt">加密字符串</param>  
  614.     /// <param name="sKey">密钥</param>  
  615.     /// <returns></returns>  
  616.     public static string string_Encrypt(string pToEncrypt, string sKey)  
  617.     {  
  618.         if (pToEncrypt == ""return "";  
  619.         if (sKey.Length < 8) sKey = sKey + "xuE29xWp";  
  620.         if (sKey.Length > 8) sKey = sKey.Substring(0, 8);  
  621.         DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
  622.         //把字符串放到byte数组中    
  623.         //原来使用的UTF8编码,我改成Unicode编码了,不行    
  624.         byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);  
  625.         //建立加密对象的密钥和偏移量    
  626.         //原文使用ASCIIEncoding.ASCII方法的GetBytes方法    
  627.         //使得输入密码必须输入英文文本    
  628.         des.Key = ASCIIEncoding.Default.GetBytes(sKey);  
  629.         des.IV = ASCIIEncoding.Default.GetBytes(sKey);  
  630.         MemoryStream ms = new MemoryStream();  
  631.         CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);  
  632.         //Write  the  byte  array  into  the  crypto  stream    
  633.         //(It  will  end  up  in  the  memory  stream)    
  634.         cs.Write(inputByteArray, 0, inputByteArray.Length);  
  635.         cs.FlushFinalBlock();  
  636.         //Get  the  data  back  from  the  memory  stream,  and  into  a  string    
  637.         StringBuilder ret = new StringBuilder();  
  638.         foreach (byte b in ms.ToArray())  
  639.         {  
  640.             //Format  as  hex    
  641.             ret.AppendFormat("{0:X2}", b);  
  642.         }  
  643.         ret.ToString();  
  644.         return ret.ToString();  
  645.     }  
  646.   
  647.     /// <summary>  
  648.     /// DES解密  
  649.     /// </summary>  
  650.     /// <param name="pToDecrypt">解密字符串</param>  
  651.     /// <param name="sKey">解密密钥</param>  
  652.     /// <param name="outstr">返回值</param>  
  653.     /// <returns></returns>   
  654.     public static bool string_Decrypt(string pToDecrypt, string sKey, out string outstr)  
  655.     {  
  656.         if (pToDecrypt == "")  
  657.         {  
  658.             outstr = "";  
  659.             return true;  
  660.         };  
  661.         if (sKey.Length < 8) sKey = sKey + "xuE29xWp";  
  662.         if (sKey.Length > 8) sKey = sKey.Substring(0, 8);  
  663.         try  
  664.         {  
  665.             DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
  666.             //Put  the  input  string  into  the  byte  array    
  667.             byte[] inputByteArray = new byte[pToDecrypt.Length / 2];  
  668.             for (int x = 0; x < pToDecrypt.Length / 2; x++)  
  669.             {  
  670.                 int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));  
  671.                 inputByteArray[x] = (byte)i;  
  672.             }  
  673.             //建立加密对象的密钥和偏移量,此值重要,不能修改    
  674.             des.Key = ASCIIEncoding.Default.GetBytes(sKey);  
  675.             des.IV = ASCIIEncoding.Default.GetBytes(sKey);  
  676.             MemoryStream ms = new MemoryStream();  
  677.             CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);  
  678.             //Flush  the  data  through  the  crypto  stream  into  the  memory  stream    
  679.             cs.Write(inputByteArray, 0, inputByteArray.Length);  
  680.             cs.FlushFinalBlock();  
  681.             //Get  the  decrypted  data  back  from  the  memory  stream    
  682.             //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象    
  683.             StringBuilder ret = new StringBuilder();  
  684.             outstr = System.Text.Encoding.Default.GetString(ms.ToArray());  
  685.             return true;  
  686.         }  
  687.         catch  
  688.         {  
  689.             outstr = "";  
  690.             return false;  
  691.         }  
  692.     }  
  693.   
  694.     /// <summary>   
  695.     /// 加密  
  696.     /// </summary>   
  697.     public class AES  
  698.     {  
  699.         //默认密钥向量  
  700.         private static byte[] Keys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F };  
  701.   
  702.         public static string Encode(string encryptString, string encryptKey)  
  703.         {  
  704.             encryptKey = GF_GET.GetSubString(encryptKey, 32, "");  
  705.             encryptKey = encryptKey.PadRight(32, ' ');  
  706.   
  707.             RijndaelManaged rijndaelProvider = new RijndaelManaged();  
  708.             rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));  
  709.             rijndaelProvider.IV = Keys;  
  710.             ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();  
  711.   
  712.             byte[] inputData = Encoding.UTF8.GetBytes(encryptString);  
  713.             byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);  
  714.   
  715.             return Convert.ToBase64String(encryptedData);  
  716.         }  
  717.   
  718.         public static string Decode(string decryptString, string decryptKey)  
  719.         {  
  720.             try  
  721.             {  
  722.                 decryptKey = GF_GET.GetSubString(decryptKey, 32, "");  
  723.                 decryptKey = decryptKey.PadRight(32, ' ');  
  724.   
  725.                 RijndaelManaged rijndaelProvider = new RijndaelManaged();  
  726.                 rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey);  
  727.                 rijndaelProvider.IV = Keys;  
  728.                 ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();  
  729.   
  730.                 byte[] inputData = Convert.FromBase64String(decryptString);  
  731.                 byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);  
  732.   
  733.                 return Encoding.UTF8.GetString(decryptedData);  
  734.             }  
  735.             catch  
  736.             {  
  737.                 return "";  
  738.             }  
  739.   
  740.         }  
  741.   
  742.     }  
  743.   
  744.     /// <summary>   
  745.     /// 加密  
  746.     /// </summary>   
  747.     public class DES  
  748.     {  
  749.         //默认密钥向量  
  750.         private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };  
  751.   
  752.         /// <summary>  
  753.         /// DES加密字符串  
  754.         /// </summary>  
  755.         /// <param name="encryptString">待加密的字符串</param>  
  756.         /// <param name="encryptKey">加密密钥,要求为8位</param>  
  757.         /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>  
  758.         public static string Encode(string encryptString, string encryptKey)  
  759.         {  
  760.             encryptKey = GF_GET.GetSubString(encryptKey, 8, "");  
  761.             encryptKey = encryptKey.PadRight(8, ' ');  
  762.             byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));  
  763.             byte[] rgbIV = Keys;  
  764.             byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);  
  765.             DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();  
  766.             MemoryStream mStream = new MemoryStream();  
  767.             CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);  
  768.             cStream.Write(inputByteArray, 0, inputByteArray.Length);  
  769.             cStream.FlushFinalBlock();  
  770.             return Convert.ToBase64String(mStream.ToArray());  
  771.   
  772.         }  
  773.   
  774.         /// <summary>  
  775.         /// DES解密字符串  
  776.         /// </summary>  
  777.         /// <param name="decryptString">待解密的字符串</param>  
  778.         /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>  
  779.         /// <returns>解密成功返回解密后的字符串,失败返源串</returns>  
  780.         public static string Decode(string decryptString, string decryptKey)  
  781.         {  
  782.             try  
  783.             {  
  784.                 decryptKey = GF_GET.GetSubString(decryptKey, 8, "");  
  785.                 decryptKey = decryptKey.PadRight(8, ' ');  
  786.                 byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);  
  787.                 byte[] rgbIV = Keys;  
  788.                 byte[] inputByteArray = Convert.FromBase64String(decryptString);  
  789.                 DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();  
  790.   
  791.                 MemoryStream mStream = new MemoryStream();  
  792.                 CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);  
  793.                 cStream.Write(inputByteArray, 0, inputByteArray.Length);  
  794.                 cStream.FlushFinalBlock();  
  795.                 return Encoding.UTF8.GetString(mStream.ToArray());  
  796.             }  
  797.             catch  
  798.             {  
  799.                 return "";  
  800.             }  
  801.         }  
  802.     }  
  803.   
  804.     /// <summary>  
  805.     /// MD5函数  
  806.     /// </summary>  
  807.     /// <param name="str">原始字符串</param>  
  808.     /// <returns>MD5结果</returns>  
  809.     public static string MD5(string str)  
  810.     {  
  811.         byte[] b = Encoding.UTF8.GetBytes(str);  
  812.         b = new MD5CryptoServiceProvider().ComputeHash(b);  
  813.         string ret = "";  
  814.         for (int i = 0; i < b.Length; i++)  
  815.             ret += b[i].ToString("x").PadLeft(2, '0');  
  816.   
  817.         return ret;  
  818.     }  
  819.   
  820.     /// <summary>  
  821.     /// SHA256函数  
  822.     /// </summary>  
  823.     /// /// <param name="str">原始字符串</param>  
  824.     /// <returns>SHA256结果</returns>  
  825.     public static string SHA256(string str)  
  826.     {  
  827.         byte[] SHA256Data = Encoding.UTF8.GetBytes(str);  
  828.         SHA256Managed Sha256 = new SHA256Managed();  
  829.         byte[] Result = Sha256.ComputeHash(SHA256Data);  
  830.         return Convert.ToBase64String(Result);  //返回长度为44字节的字符串  
  831.     }  
  832. }  
  833.  
  834. #endregion  
  835. #region 读取ini文件  
  836. //读写INI  
  837. public class GF_INI  
  838. {  
  839.     [DllImport("kernel32")]  
  840.     private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);  
  841.     [DllImport("kernel32")]  
  842.     private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);  
  843.     [DllImport("kernel32.dll")]  
  844.     public static extern int Beep(int dwFreq, int dwDuration);  
  845.   
  846.     //读ini  
  847.     public static void iniFile_SetVal(string in_filename, string Section, string Key, string Value)  
  848.     {  
  849.         WritePrivateProfileString(Section, Key, Value, in_filename);  
  850.     }  
  851.   
  852.     //写INI  
  853.     public static string iniFile_GetVal(string in_filename, string Section, string Key)  
  854.     {  
  855.         StringBuilder temp = new StringBuilder(255);  
  856.         int i = GetPrivateProfileString(Section, Key, "", temp, 255, in_filename);  
  857.         if (i == 0)  
  858.             return "";  
  859.         else  
  860.             return temp.ToString();  
  861.     }  
  862. }  
  863. #endregion  
  864. #region 硬件信息  
  865. //硬件信息  
  866. public class GF_Hardware  
  867. {  
  868.     /// <summary>  
  869.     /// cpu序列号  
  870.     /// </summary>  
  871.     /// <returns></returns>  
  872.     public static string getID_CpuId()  
  873.     {  
  874.   
  875.         string cpuInfo = "";//cpu序列号  
  876.         ManagementClass cimobject = new ManagementClass("Win32_Processor");  
  877.         ManagementObjectCollection moc = cimobject.GetInstances();  
  878.         foreach (ManagementObject mo in moc)  
  879.         {  
  880.             cpuInfo = mo.Properties["ProcessorId"].Value.ToString();  
  881.         }  
  882.         return cpuInfo;  
  883.     }  
  884.   
  885.     /// <summary>  
  886.     /// 硬盘ID号  
  887.     /// </summary>  
  888.     /// <returns></returns>  
  889.     public static string getID_HardDiskId()  
  890.     {  
  891.         string HDid = "";  
  892.         ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");  
  893.         ManagementObjectCollection moc = cimobject.GetInstances();  
  894.         foreach (ManagementObject mo in moc)  
  895.         {  
  896.             HDid = (string)mo.Properties["Model"].Value;  
  897.         }  
  898.         return HDid;  
  899.     }  
  900.   
  901.     /// <summary>  
  902.     /// 获取网卡MacAddress  
  903.     /// </summary>  
  904.     /// <returns></returns>  
  905.     public static string getID_NetCardId()  
  906.     {  
  907.         string NCid = "";  
  908.         ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");  
  909.         ManagementObjectCollection moc = mc.GetInstances();  
  910.         foreach (ManagementObject mo in moc)  
  911.         {  
  912.             if ((bool)mo["IPEnabled"] == true)  
  913.                 NCid = mo["MacAddress"].ToString();  
  914.             mo.Dispose();  
  915.         }  
  916.         return NCid;  
  917.     }  
  918.   
  919.   
  920.   
  921. }  
  922. #endregion  
  923. #region 网络部分  
  924. //网络部分  
  925. public class GF_Network  
  926. {  
  927.     /* 
  928.      * C#完整的通信代码(点对点,点对多,同步,异步,UDP,TCP)    
  929.      * http://topic.csdn.net/u/20080619/08/dcef3fe2-f95b-4918-8edb-36d48a3d0528_2.html 
  930.      *  
  931.      */  
  932.   
  933.   
  934.     /// <summary>  
  935.     /// 获取IP地址 返回第一个  
  936.     /// </summary>  
  937.     /// <returns></returns>  
  938.     public static string getIP_This()  
  939.     {  
  940.         IPHostEntry hostInfo = Dns.GetHostEntry(Dns.GetHostName());  
  941.         IPAddress[] address = hostInfo.AddressList;  
  942.         if (address.Length == 0)  
  943.             return "";  
  944.         else  
  945.             return address[0].ToString();  
  946.     }  
  947.   
  948.     /// <summary>  
  949.     /// ping IP地址 timeout 局域网用200,广域网用2000  
  950.     /// </summary>  
  951.     /// <param name="ip">IP地址</param>  
  952.     /// <param name="timeout">超时 毫秒</param>  
  953.     /// <returns></returns>  
  954.     public static bool ping(string ip, int timeout)  
  955.     {  
  956.         IPAddress ipadd;  
  957.         if (!IPAddress.TryParse(ip, out ipadd))  
  958.         {  
  959.             return false;  
  960.         }  
  961.         Ping pingSender = new Ping();  
  962.         PingReply reply = pingSender.Send(ip, timeout, new Byte[] { Convert.ToByte(1) });  
  963.         if (reply.Status == IPStatus.Success)  
  964.             return true;  
  965.         else  
  966.             return false;  
  967.     }  
  968.     /// <summary>  
  969.     /// 判读是否是IP地址  
  970.     /// </summary>  
  971.     /// <param name="in_str"></param>  
  972.     /// <returns></returns>  
  973.     public static bool IsIPStr(string in_str)  
  974.     {  
  975.         if (in_str.Replace(".""").Length != in_str.Length - 3)  
  976.             return false;  
  977.         try  
  978.         {  
  979.             IPAddress ip = IPAddress.Parse(in_str);  
  980.             return true;  
  981.         }  
  982.         catch  
  983.         {  
  984.             return false;  
  985.         }  
  986.     }  
  987.   
  988.   
  989. }  
  990.  
  991. #endregion  
  992. #region 文件操作  
  993. //文件操作  
  994. public class GF_File  
  995. {  
  996.   
  997.     /// <summary>  
  998.     /// 写日志文件  
  999.     /// </summary>  
  1000.     /// <param name="sPath">    年月  例  2011-04</param>  
  1001.     /// <param name="sFileName">月日  例  04-22</param>  
  1002.     /// <param name="content">时间+  内容</param>  
  1003.     /// <returns></returns>  
  1004.     public static bool WriteLog(string sPath, string sFileName, string content)  
  1005.     {  
  1006.         try  
  1007.         {  
  1008.   
  1009.   
  1010.             StreamWriter sr;  
  1011.             if (!Directory.Exists(sPath))  
  1012.             {  
  1013.                 Directory.CreateDirectory(sPath);  
  1014.             }  
  1015.             string v_filename = sPath + "\\" + sFileName;  
  1016.   
  1017.   
  1018.             if (!File.Exists(v_filename)) //如果文件存在,则创建File.AppendText对象  
  1019.             {  
  1020.                 sr = File.CreateText(v_filename);  
  1021.                 sr.Close();  
  1022.             }  
  1023.             using (FileStream fs = new FileStream(v_filename, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Write))  
  1024.             {  
  1025.                 using (sr = new StreamWriter(fs))  
  1026.                 {  
  1027.   
  1028.                     sr.WriteLine(DateTime.Now.ToString("hh:mm:ss") + "     " + content);  
  1029.                     sr.Close();  
  1030.                 }  
  1031.                 fs.Close();  
  1032.             }  
  1033.             return true;  
  1034.   
  1035.         }  
  1036.         catch { return false; }  
  1037.     }  
  1038.   
  1039.   
  1040.     /// <summary>  
  1041.     /// 读取文本文件内容,每行存入arrayList 并返回arrayList对象  
  1042.     /// </summary>  
  1043.     /// <param name="sFileName"></param>  
  1044.     /// <returns>arrayList</returns>  
  1045.     public static ArrayList ReadFileRow(string sFileName)  
  1046.     {  
  1047.         string sLine = "";  
  1048.         ArrayList alTxt = null;  
  1049.         try  
  1050.         {  
  1051.             using (StreamReader sr = new StreamReader(sFileName))  
  1052.             {  
  1053.                 alTxt = new ArrayList();  
  1054.   
  1055.                 while (!sr.EndOfStream)  
  1056.                 {  
  1057.                     sLine = sr.ReadLine();  
  1058.                     if (sLine != "")  
  1059.                     {  
  1060.                         alTxt.Add(sLine.Trim());  
  1061.                     }  
  1062.   
  1063.                 }  
  1064.                 sr.Close();  
  1065.             }  
  1066.         }  
  1067.         catch  
  1068.         {  
  1069.   
  1070.         }  
  1071.         return alTxt;  
  1072.     }  
  1073.   
  1074.   
  1075.     /// <summary>  
  1076.     /// 备份文件  
  1077.     /// </summary>  
  1078.     /// <param name="sourceFileName">源文件名</param>  
  1079.     /// <param name="destFileName">目标文件名</param>  
  1080.     /// <param name="overwrite">当目标文件存在时是否覆盖</param>  
  1081.     /// <returns>操作是否成功</returns>  
  1082.     public static bool BackupFile(string sourceFileName, string destFileName, bool overwrite)  
  1083.     {  
  1084.         if (!System.IO.File.Exists(sourceFileName))  
  1085.             throw new FileNotFoundException(sourceFileName + "文件不存在!");  
  1086.   
  1087.         if (!overwrite && System.IO.File.Exists(destFileName))  
  1088.             return false;  
  1089.   
  1090.         try  
  1091.         {  
  1092.             System.IO.File.Copy(sourceFileName, destFileName, true);  
  1093.             return true;  
  1094.         }  
  1095.         catch (Exception e)  
  1096.         {  
  1097.             throw e;  
  1098.         }  
  1099.     }  
  1100.   
  1101.   
  1102.     /// <summary>  
  1103.     /// 备份文件,当目标文件存在时覆盖  
  1104.     /// </summary>  
  1105.     /// <param name="sourceFileName">源文件名</param>  
  1106.     /// <param name="destFileName">目标文件名</param>  
  1107.     /// <returns>操作是否成功</returns>  
  1108.     public static bool BackupFile(string sourceFileName, string destFileName)  
  1109.     {  
  1110.         return BackupFile(sourceFileName, destFileName, true);  
  1111.     }  
  1112.   
  1113.   
  1114.     /// <summary>  
  1115.     /// 恢复文件  
  1116.     /// </summary>  
  1117.     /// <param name="backupFileName">备份文件名</param>  
  1118.     /// <param name="targetFileName">要恢复的文件名</param>  
  1119.     /// <param name="backupTargetFileName">要恢复文件再次备份的名称,如果为null,则不再备份恢复文件</param>  
  1120.     /// <returns>操作是否成功</returns>  
  1121.     public static bool RestoreFile(string backupFileName, string targetFileName, string backupTargetFileName)  
  1122.     {  
  1123.         try  
  1124.         {  
  1125.             if (!System.IO.File.Exists(backupFileName))  
  1126.                 throw new FileNotFoundException(backupFileName + "文件不存在!");  
  1127.   
  1128.             if (backupTargetFileName != null)  
  1129.             {  
  1130.                 if (!System.IO.File.Exists(targetFileName))  
  1131.                     throw new FileNotFoundException(targetFileName + "文件不存在!无法备份此文件!");  
  1132.                 else  
  1133.                     System.IO.File.Copy(targetFileName, backupTargetFileName, true);  
  1134.             }  
  1135.             System.IO.File.Delete(targetFileName);  
  1136.             System.IO.File.Copy(backupFileName, targetFileName);  
  1137.         }  
  1138.         catch (Exception e)  
  1139.         {  
  1140.             throw e;  
  1141.         }  
  1142.         return true;  
  1143.     }  
  1144.   
  1145.     public static bool RestoreFile(string backupFileName, string targetFileName)  
  1146.     {  
  1147.         return RestoreFile(backupFileName, targetFileName, null);  
  1148.     }  
  1149. }  
  1150.  
  1151. #endregion  
  1152. #region 屏幕操作  
  1153. //获取部分  
  1154. public class GF_GET  
  1155. {  
  1156.     /// <summary>  
  1157.     /// 根据坐标点获取屏幕图像  
  1158.     /// </summary>  
  1159.     /// <param name="x1">左上角横坐标</param>  
  1160.     /// <param name="y1">左上角纵坐标</param>  
  1161.     /// <param name="x2">右下角横坐标</param>  
  1162.     /// <param name="y2">右下角纵坐标</param>  
  1163.     /// <returns></returns>  
  1164.     public static Image GetScreen(int x1, int y1, int x2, int y2)  
  1165.     {  
  1166.         int w = (x2 - x1);  
  1167.         int h = (y2 - y1);  
  1168.         Image myImage = new Bitmap(w, h);  
  1169.         Graphics g = Graphics.FromImage(myImage);  
  1170.         g.CopyFromScreen(new Point(x1, y1), new Point(0, 0), new Size(w, h));  
  1171.         IntPtr dc1 = g.GetHdc();  
  1172.         g.ReleaseHdc(dc1);  
  1173.         return myImage;  
  1174.     }  
  1175.   
  1176.     /// <summary>  
  1177.     /// 获取指定文件的扩展名 例:  .txt  
  1178.     /// </summary>  
  1179.     /// <param name="fileName">指定文件名</param>  
  1180.     /// <returns>扩展名</returns>  
  1181.     public static string GetFileExtName(string fileName)  
  1182.     {  
  1183.         if (GF_IsOk.IsStrNullOrEmpty(fileName) || fileName.IndexOf('.') <= 0)  
  1184.             return "";  
  1185.   
  1186.         fileName = fileName.ToLower().Trim();  
  1187.   
  1188.   
  1189.         return fileName.Substring(fileName.LastIndexOf('.'), fileName.Length - fileName.LastIndexOf('.'));  
  1190.     }  
  1191.   
  1192.     public static string GetSubString(string p_SrcString, int p_Length, string p_TailString)  
  1193.     {  
  1194.         return GetSubString(p_SrcString, 0, p_Length, p_TailString);  
  1195.     }  
  1196.   
  1197.     public static string GetUnicodeSubString(string str, int len, string p_TailString)  
  1198.     {  
  1199.         string result = string.Empty;// 最终返回的结果  
  1200.         int byteLen = System.Text.Encoding.Default.GetByteCount(str);// 单字节字符长度  
  1201.         int charLen = str.Length;// 把字符平等对待时的字符串长度  
  1202.         int byteCount = 0;// 记录读取进度  
  1203.         int pos = 0;// 记录截取位置  
  1204.         if (byteLen > len)  
  1205.         {  
  1206.             for (int i = 0; i < charLen; i++)  
  1207.             {  
  1208.                 if (Convert.ToInt32(str.ToCharArray()[i]) > 255)// 按中文字符计算加2  
  1209.                     byteCount += 2;  
  1210.                 else// 按英文字符计算加1  
  1211.                     byteCount += 1;  
  1212.                 if (byteCount > len)// 超出时只记下上一个有效位置  
  1213.                 {  
  1214.                     pos = i;  
  1215.                     break;  
  1216.                 }  
  1217.                 else if (byteCount == len)// 记下当前位置  
  1218.                 {  
  1219.                     pos = i + 1;  
  1220.                     break;  
  1221.                 }  
  1222.             }  
  1223.   
  1224.             if (pos >= 0)  
  1225.                 result = str.Substring(0, pos) + p_TailString;  
  1226.         }  
  1227.         else  
  1228.             result = str;  
  1229.   
  1230.         return result;  
  1231.     }  
  1232.   
  1233.     /// <summary>  
  1234.     /// 取指定长度的字符串  
  1235.     /// </summary>  
  1236.     /// <param name="p_SrcString">要检查的字符串</param>  
  1237.     /// <param name="p_StartIndex">起始位置</param>  
  1238.     /// <param name="p_Length">指定长度</param>  
  1239.     /// <param name="p_TailString">用于替换的字符串</param>  
  1240.     /// <returns>截取后的字符串</returns>  
  1241.     public static string GetSubString(string p_SrcString, int p_StartIndex, int p_Length, string p_TailString)  
  1242.     {  
  1243.         string myResult = p_SrcString;  
  1244.   
  1245.         Byte[] bComments = Encoding.UTF8.GetBytes(p_SrcString);  
  1246.         foreach (char c in Encoding.UTF8.GetChars(bComments))  
  1247.         {    //当是日文或韩文时(注:中文的范围:\u4e00 - \u9fa5, 日文在\u0800 - \u4e00, 韩文为\xAC00-\xD7A3)  
  1248.             if ((c > '\u0800' && c < '\u4e00') || (c > '\xAC00' && c < '\xD7A3'))  
  1249.             {  
  1250.                 //if (System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\u0800-\u4e00]+") || System.Text.RegularExpressions.Regex.IsMatch(p_SrcString, "[\xAC00-\xD7A3]+"))  
  1251.                 //当截取的起始位置超出字段串长度时  
  1252.                 if (p_StartIndex >= p_SrcString.Length)  
  1253.                     return "";  
  1254.                 else  
  1255.                     return p_SrcString.Substring(p_StartIndex,  
  1256.                                                    ((p_Length + p_StartIndex) > p_SrcString.Length) ? (p_SrcString.Length - p_StartIndex) : p_Length);  
  1257.             }  
  1258.         }  
  1259.   
  1260.         if (p_Length >= 0)  
  1261.         {  
  1262.             byte[] bsSrcString = Encoding.Default.GetBytes(p_SrcString);  
  1263.   
  1264.             //当字符串长度大于起始位置  
  1265.             if (bsSrcString.Length > p_StartIndex)  
  1266.             {  
  1267.                 int p_EndIndex = bsSrcString.Length;  
  1268.   
  1269.                 //当要截取的长度在字符串的有效长度范围内  
  1270.                 if (bsSrcString.Length > (p_StartIndex + p_Length))  
  1271.                 {  
  1272.                     p_EndIndex = p_Length + p_StartIndex;  
  1273.                 }  
  1274.                 else  
  1275.                 {   //当不在有效范围内时,只取到字符串的结尾  
  1276.   
  1277.                     p_Length = bsSrcString.Length - p_StartIndex;  
  1278.                     p_TailString = "";  
  1279.                 }  
  1280.   
  1281.                 int nRealLength = p_Length;  
  1282.                 int[] anResultFlag = new int[p_Length];  
  1283.                 byte[] bsResult = null;  
  1284.   
  1285.                 int nFlag = 0;  
  1286.                 for (int i = p_StartIndex; i < p_EndIndex; i++)  
  1287.                 {  
  1288.                     if (bsSrcString[i] > 127)  
  1289.                     {  
  1290.                         nFlag++;  
  1291.                         if (nFlag == 3)  
  1292.                             nFlag = 1;  
  1293.                     }  
  1294.                     else  
  1295.                         nFlag = 0;  
  1296.   
  1297.                     anResultFlag[i] = nFlag;  
  1298.                 }  
  1299.   
  1300.                 if ((bsSrcString[p_EndIndex - 1] > 127) && (anResultFlag[p_Length - 1] == 1))  
  1301.                     nRealLength = p_Length + 1;  
  1302.   
  1303.                 bsResult = new byte[nRealLength];  
  1304.   
  1305.                 Array.Copy(bsSrcString, p_StartIndex, bsResult, 0, nRealLength);  
  1306.   
  1307.                 myResult = Encoding.Default.GetString(bsResult);  
  1308.                 myResult = myResult + p_TailString;  
  1309.             }  
  1310.         }  
  1311.   
  1312.         return myResult;  
  1313.     }  
  1314.   
  1315.     /// <summary>  
  1316.     /// 获取Email HostName 例 liyangfd@gmail.com   获取出来时@gmail.com  
  1317.     /// </summary>  
  1318.     /// <param name="strEmail"></param>  
  1319.     /// <returns></returns>  
  1320.     public static string GetEmailHostName(string strEmail)  
  1321.     {  
  1322.         if (strEmail.IndexOf("@") < 0)  
  1323.         {  
  1324.             return "";  
  1325.         }  
  1326.         return strEmail.Substring(strEmail.LastIndexOf("@")).ToLower();  
  1327.     }  
  1328.   
  1329.     /// <summary>  
  1330.     /// 返回URL中结尾的文件名  
  1331.     /// </summary>          
  1332.     public static string GetFilename(string url)  
  1333.     {  
  1334.         if (url == null)  
  1335.         {  
  1336.             return "";  
  1337.         }  
  1338.         string[] strs1 = url.Split(new char[] { '/' });  
  1339.         return strs1[strs1.Length - 1].Split(new char[] { '?' })[0];  
  1340.     }  
  1341.   
  1342.   
  1343.     /// <summary>  
  1344.     /// 根据阿拉伯数字返回月份的名称(可更改为某种语言)  
  1345.     /// </summary>      
  1346.     public static string[] Monthes  
  1347.     {  
  1348.         get  
  1349.         {  
  1350.             return new string[] { "January""February""March""April""May""June""July""August""September""October""November""December" };  
  1351.         }  
  1352.     }  
  1353. }  
  1354.  
  1355. #endregion  
  1356. #region 判断操作  
  1357. //判断部分  
  1358. public class GF_IsOk  
  1359. {  
  1360.     /// <summary>  
  1361.     /// 判读是否是IP地址  
  1362.     /// </summary>  
  1363.     /// <param name="in_str"></param>  
  1364.     /// <returns></returns>  
  1365.     public static bool IsIPStr(string in_str)  
  1366.     {  
  1367.         IPAddress ip;  
  1368.         return IPAddress.TryParse(in_str, out ip);  
  1369.     }  
  1370.   
  1371.     /// <summary>  
  1372.     /// 判断是否是数字  
  1373.     /// </summary>  
  1374.     /// <param name="strNumber"></param>  
  1375.     /// <returns></returns>  
  1376.     public static bool IsNumber(string strNumber)  
  1377.     {  
  1378.   
  1379.         Regex objNotNumberPattern = new Regex("[^0-9.-]");  
  1380.         Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");  
  1381.         Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");  
  1382.         String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";  
  1383.         String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";  
  1384.         Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");  
  1385.         return !objNotNumberPattern.IsMatch(strNumber) &&  
  1386.                !objTwoDotPattern.IsMatch(strNumber) &&  
  1387.                !objTwoMinusPattern.IsMatch(strNumber) &&  
  1388.                objNumberPattern.IsMatch(strNumber);  
  1389.     }  
  1390.   
  1391.     /// <summary>  
  1392.     ///  判断是否是日期字符串  
  1393.     /// </summary>  
  1394.     /// <param name="in_str"></param>  
  1395.     /// <returns></returns>  
  1396.     public static bool IsDateStr_yyyymmdd(string in_str)  
  1397.     {  
  1398.         if (in_str == ""return true;  
  1399.         if (in_str.Length != 8) return false;  
  1400.         return IsDateStr(in_str);  
  1401.     }  
  1402.   
  1403.     /// <summary>  
  1404.     /// 判断是否是日期字符串  
  1405.     /// </summary>  
  1406.     /// <param name="in_str"></param>  
  1407.     /// <returns></returns>  
  1408.     public static bool IsDateStr(string in_str)  
  1409.     {  
  1410.         if (in_str == ""return true;  
  1411.         if (in_str.Length == 8)  
  1412.             in_str = in_str.Substring(0, 4) + "-" + in_str.Substring(4, 2) + "-" + in_str.Substring(6, 2);  
  1413.         DateTime dtDate;  
  1414.         bool bValid = true;  
  1415.         try  
  1416.         {  
  1417.             dtDate = DateTime.Parse(in_str);  
  1418.         }  
  1419.         catch (FormatException)  
  1420.         {  
  1421.             // 如果解析方法失败则表示不是日期性数据  
  1422.             bValid = false;  
  1423.         }  
  1424.         return bValid;  
  1425.     }  
  1426.   
  1427.     /// <summary>  
  1428.     /// 判断字符串中是否包含汉字,有返回true 否则为false  
  1429.     /// </summary>  
  1430.     /// <param name="str"></param>  
  1431.     /// <returns></returns>  
  1432.     public static bool IsExistHanZi(string str)  
  1433.     {  
  1434.         Regex reg = new Regex(@"[\u4e00-\u9fa5]");//正则表达式  
  1435.         if (reg.IsMatch(str))  
  1436.         {  
  1437.             return true;  
  1438.         }  
  1439.         else  
  1440.         {  
  1441.             return false;  
  1442.         }  
  1443.     }  
  1444.   
  1445.   
  1446.     /// <summary>  
  1447.     /// 字段串是否为Null或为""(空)  
  1448.     /// </summary>  
  1449.     /// <param name="str"></param>  
  1450.     /// <returns></returns>  
  1451.     public static bool IsStrNullOrEmpty(string str)  
  1452.     {  
  1453.         if (str == null || str.Trim() == string.Empty)  
  1454.             return true;  
  1455.   
  1456.         return false;  
  1457.     }  
  1458.   
  1459.     /// <summary>  
  1460.     /// 返回文件是否存在  
  1461.     /// </summary>  
  1462.     /// <param name="filename">文件名</param>  
  1463.     /// <returns>是否存在</returns>  
  1464.     public static bool IsFileExists(string filename)  
  1465.     {  
  1466.         return System.IO.File.Exists(filename);  
  1467.     }  
  1468.   
  1469.   
  1470.     /// <summary>  
  1471.     /// 检测是否符合email格式  
  1472.     /// </summary>  
  1473.     /// <param name="strEmail">要判断的email字符串</param>  
  1474.     /// <returns>判断结果</returns>  
  1475.     public static bool IsValidEmail(string strEmail)  
  1476.     {  
  1477.         return Regex.IsMatch(strEmail, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]");  
  1478.     }  
  1479.   
  1480.     public static bool IsValidDoEmail(string strEmail)  
  1481.     {  
  1482.         return Regex.IsMatch(strEmail, @"^@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");  
  1483.     }  
  1484.     /// <summary>  
  1485.     /// 检测是否是正确的Url  
  1486.     /// </summary>  
  1487.     /// <param name="strUrl">要验证的Url</param>  
  1488.     /// <returns>判断结果</returns>  
  1489.     public static bool IsURL(string strUrl)  
  1490.     {  
  1491.         return Regex.IsMatch(strUrl, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");  
  1492.     }  
  1493.   
  1494.     /// <summary>  
  1495.     /// 判断是否为base64字符串  
  1496.     /// </summary>  
  1497.     /// <param name="str"></param>  
  1498.     /// <returns></returns>  
  1499.     public static bool IsBase64String(string str)  
  1500.     {  
  1501.         //A-Z, a-z, 0-9, +, /, =  
  1502.         return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");  
  1503.     }  
  1504.   
  1505.     /// <summary>  
  1506.     /// 检测是否有Sql危险字符  
  1507.     /// </summary>  
  1508.     /// <param name="str">要判断字符串</param>  
  1509.     /// <returns>判断结果</returns>  
  1510.     public static bool IsSafeSqlString(string str)  
  1511.     {  
  1512.         return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");  
  1513.     }  
  1514.   
  1515.   
  1516.   
  1517.   
  1518.   
  1519. }  
  1520. #endregion  
  1521. #region 数据库操作  
  1522. //数据库  
  1523. public class GF_DA  
  1524. {  
  1525.     /// <summary>  
  1526.     /// 执行SQL语句 sConnStr 连接字符串,sql执行的sql命令 返回第一行第一列  
  1527.     /// </summary>  
  1528.     /// <param name="sConnStr"></param>  
  1529.     /// <param name="sql"></param>  
  1530.     /// <returns></returns>  
  1531.     public static object ExecSQL(string sConnStr, string sql)  
  1532.     {  
  1533.         using (SqlConnection conn = new SqlConnection(sConnStr))  
  1534.         {  
  1535.             using (SqlCommand cmd = new SqlCommand())  
  1536.             {  
  1537.                 try  
  1538.                 {  
  1539.                     conn.Open();  
  1540.                     cmd.Connection = conn;  
  1541.                     cmd.CommandText = sql;  
  1542.                     return cmd.ExecuteScalar();  
  1543.                 }  
  1544.                 catch  
  1545.                 {  
  1546.                     return null;  
  1547.                 }  
  1548.             }  
  1549.   
  1550.         }  
  1551.     }  
  1552.   
  1553.     /// <summary>  
  1554.     /// 执行SQL数组    
  1555.     /// </summary>  
  1556.     /// <param name="sConnStr"></param>  
  1557.     /// <param name="alSql"></param>  
  1558.     /// <param name="iLen"></param>  
  1559.     public static void ExecSQL(string sConnStr, ArrayList alSql, int iLen)  
  1560.     {  
  1561.         using (SqlConnection conn = new SqlConnection(sConnStr))  
  1562.         {  
  1563.             using (SqlCommand cmd = new SqlCommand())  
  1564.             {  
  1565.                 try  
  1566.                 {  
  1567.                     conn.Open();  
  1568.                     cmd.Connection = conn;  
  1569.                     for (int i = 0; i < alSql.Count; i++)  
  1570.                     {  
  1571.                         cmd.CommandText = alSql[i].ToString();  
  1572.                         cmd.ExecuteNonQuery();  
  1573.   
  1574.                     }  
  1575.                 }  
  1576.                 catch  
  1577.                 {  
  1578.   
  1579.                 }  
  1580.             }  
  1581.   
  1582.         }  
  1583.     }  
  1584.   
  1585.     /// <summary>  
  1586.     /// 填充数据 返回DataTable  
  1587.     /// </summary>  
  1588.     /// <param name="sConnStr"></param>  
  1589.     /// <param name="sql"></param>  
  1590.     /// <param name="sTableName"></param>  
  1591.     /// <returns></returns>  
  1592.     public static DataTable DataFill(string sConnStr, string sql, string sTableName)  
  1593.     {  
  1594.         using (SqlConnection conn = new SqlConnection(sConnStr))  
  1595.         {  
  1596.             using (SqlCommand cmd = new SqlCommand())  
  1597.             {  
  1598.                 DataSet ds = new DataSet();  
  1599.                 try  
  1600.                 {  
  1601.                     conn.Open();  
  1602.                     cmd.Connection = conn;  
  1603.                     cmd.CommandText = sql;  
  1604.                     SqlDataAdapter ap = new SqlDataAdapter(cmd);  
  1605.                     ap.Fill(ds, sTableName);  
  1606.                     return ds.Tables[0];  
  1607.                 }  
  1608.                 catch  
  1609.                 {  
  1610.                     return null;  
  1611.                 }  
  1612.             }  
  1613.   
  1614.   
  1615.         }  
  1616.     }  
  1617. }  
  1618. #endregion  
  1619. #region 各种进制转换  
  1620. public sealed class DataConvert  
  1621. {  
  1622.     #region Helperfunctions  
  1623.   
  1624.     /// <summary>  
  1625.     /// 十进制转换为二进制  
  1626.     /// </summary>  
  1627.     /// <param name="x"></param>  
  1628.     /// <returns></returns>  
  1629.     public static string DecToBin(string x)  
  1630.     {  
  1631.         string z = null;  
  1632.         int X = Convert.ToInt32(x);  
  1633.         int i = 0;  
  1634.         long a, b = 0;  
  1635.         while (X > 0)  
  1636.         {  
  1637.             a = X % 2;  
  1638.             X = X / 2;  
  1639.             b = b + a * Pow(10, i);  
  1640.             i++;  
  1641.         }  
  1642.         z = Convert.ToString(b);  
  1643.         return z;  
  1644.     }  
  1645.   
  1646.     /// <summary>  
  1647.     /// 16进制转ASCII码  
  1648.     /// </summary>  
  1649.     /// <param name="hexString"></param>  
  1650.     /// <returns></returns>  
  1651.     public static string HexToAscii(string hexString)  
  1652.     {  
  1653.         StringBuilder sb = new StringBuilder();  
  1654.         for (int i = 0; i <= hexString.Length - 2; i += 2)  
  1655.         {  
  1656.             sb.Append(  
  1657.                 Convert.ToString(  
  1658.                     Convert.ToChar(Int32.Parse(hexString.Substring(i, 2),  
  1659.                                                System.Globalization.NumberStyles.HexNumber))));  
  1660.         }  
  1661.         return sb.ToString();  
  1662.     }  
  1663.   
  1664.     /// <summary>  
  1665.     /// 十进制转换为八进制  
  1666.     /// </summary>  
  1667.     /// <param name="x"></param>  
  1668.     /// <returns></returns>  
  1669.     public static string DecToOtc(string x)  
  1670.     {  
  1671.         string z = null;  
  1672.         int X = Convert.ToInt32(x);  
  1673.         int i = 0;  
  1674.         long a, b = 0;  
  1675.         while (X > 0)  
  1676.         {  
  1677.             a = X % 8;  
  1678.             X = X / 8;  
  1679.             b = b + a * Pow(10, i);  
  1680.             i++;  
  1681.         }  
  1682.         z = Convert.ToString(b);  
  1683.         return z;  
  1684.     }  
  1685.   
  1686.     /// <summary>  
  1687.     /// 十进制转换为十六进制  
  1688.     /// </summary>  
  1689.     /// <param name="x"></param>  
  1690.     /// <returns></returns>  
  1691.     public static string DecToHex(string x)  
  1692.     {  
  1693.         if (string.IsNullOrEmpty(x))  
  1694.         {  
  1695.             return "0";  
  1696.         }  
  1697.         string z = null;  
  1698.         int X = Convert.ToInt32(x);  
  1699.         Stack a = new Stack();  
  1700.         int i = 0;  
  1701.         while (X > 0)  
  1702.         {  
  1703.             a.Push(Convert.ToString(X % 16));  
  1704.             X = X / 16;  
  1705.             i++;  
  1706.         }  
  1707.         while (a.Count != 0)  
  1708.             z += ToHex(Convert.ToString(a.Pop()));  
  1709.         if (string.IsNullOrEmpty(z))  
  1710.         {  
  1711.             z = "0";  
  1712.         }  
  1713.         return z;  
  1714.     }  
  1715.   
  1716.     /// <summary>  
  1717.     /// 二进制转换为十进制  
  1718.     /// </summary>  
  1719.     /// <param name="x"></param>  
  1720.     /// <returns></returns>  
  1721.     public static string BinToDec(string x)  
  1722.     {  
  1723.         string z = null;  
  1724.         int X = Convert.ToInt32(x);  
  1725.         int i = 0;  
  1726.         long a, b = 0;  
  1727.         while (X > 0)  
  1728.         {  
  1729.             a = X % 10;  
  1730.             X = X / 10;  
  1731.             b = b + a * Pow(2, i);  
  1732.             i++;  
  1733.         }  
  1734.         z = Convert.ToString(b);  
  1735.         return z;  
  1736.     }  
  1737.   
  1738.     /// <summary>  
  1739.     /// 二进制转换为十进制,定长转换  
  1740.     /// </summary>  
  1741.     /// <param name="x"></param>  
  1742.     /// <param name="iLength"></param>  
  1743.     /// <returns></returns>  
  1744.     public static string BinToDec(string x, short iLength)  
  1745.     {  
  1746.         StringBuilder sb = new StringBuilder();  
  1747.         int iCount = 0;  
  1748.   
  1749.         iCount = x.Length / iLength;  
  1750.   
  1751.         if (x.Length % iLength > 0)  
  1752.         {  
  1753.             iCount += 1;  
  1754.         }  
  1755.   
  1756.         int X = 0;  
  1757.   
  1758.         for (int i = 0; i < iCount; i++)  
  1759.         {  
  1760.             if ((i + 1) * iLength > x.Length)  
  1761.             {  
  1762.                 X = Convert.ToInt32(x.Substring(i * iLength, (x.Length - iLength)));  
  1763.             }  
  1764.             else  
  1765.             {  
  1766.                 X = Convert.ToInt32(x.Substring(i * iLength, iLength));  
  1767.             }  
  1768.             int j = 0;  
  1769.             long a, b = 0;  
  1770.             while (X > 0)  
  1771.             {  
  1772.                 a = X % 10;  
  1773.                 X = X / 10;  
  1774.                 b = b + a * Pow(2, j);  
  1775.                 j++;  
  1776.             }  
  1777.             sb.AppendFormat("{0:D2}", b);  
  1778.         }  
  1779.         return sb.ToString();  
  1780.     }  
  1781.   
  1782.     /// <summary>  
  1783.     /// 二进制转换为十六进制,定长转换  
  1784.     /// </summary>  
  1785.     /// <param name="x"></param>  
  1786.     /// <param name="iLength"></param>  
  1787.     /// <returns></returns>  
  1788.     public static string BinToHex(string x, short iLength)  
  1789.     {  
  1790.         StringBuilder sb = new StringBuilder();  
  1791.         int iCount = 0;  
  1792.   
  1793.         iCount = x.Length / iLength;  
  1794.   
  1795.         if (x.Length % iLength > 0)  
  1796.         {  
  1797.             iCount += 1;  
  1798.         }  
  1799.   
  1800.         int X = 0;  
  1801.   
  1802.         for (int i = 0; i < iCount; i++)  
  1803.         {  
  1804.             if ((i + 1) * iLength > x.Length)  
  1805.             {  
  1806.                 X = Convert.ToInt32(x.Substring(i * iLength, (x.Length - iLength)));  
  1807.             }  
  1808.             else  
  1809.             {  
  1810.                 X = Convert.ToInt32(x.Substring(i * iLength, iLength));  
  1811.             }  
  1812.             int j = 0;  
  1813.             long a, b = 0;  
  1814.             while (X > 0)  
  1815.             {  
  1816.                 a = X % 10;  
  1817.                 X = X / 10;  
  1818.                 b = b + a * Pow(2, j);  
  1819.                 j++;  
  1820.             }  
  1821.             //前补0  
  1822.             sb.Append(DecToHex(b.ToString()));  
  1823.         }  
  1824.         return sb.ToString();  
  1825.     }  
  1826.   
  1827.     /// <summary>  
  1828.     /// 八进制转换为十进制  
  1829.     /// </summary>  
  1830.     /// <param name="x"></param>  
  1831.     /// <returns></returns>  
  1832.     public static string OctToDec(string x)  
  1833.     {  
  1834.         string z = null;  
  1835.         int X = Convert.ToInt32(x);  
  1836.         int i = 0;  
  1837.         long a, b = 0;  
  1838.         while (X > 0)  
  1839.         {  
  1840.             a = X % 10;  
  1841.             X = X / 10;  
  1842.             b = b + a * Pow(8, i);  
  1843.             i++;  
  1844.         }  
  1845.         z = Convert.ToString(b);  
  1846.         return z;  
  1847.     }  
  1848.   
  1849.   
  1850.     /// <summary>  
  1851.     /// 十六进制转换为十进制  
  1852.     /// </summary>  
  1853.     /// <param name="x"></param>  
  1854.     /// <returns></returns>  
  1855.     public static string HexToDec(string x)  
  1856.     {  
  1857.         if (string.IsNullOrEmpty(x))  
  1858.         {  
  1859.             return "0";  
  1860.         }  
  1861.         string z = null;  
  1862.         Stack a = new Stack();  
  1863.         int i = 0, j = 0, l = x.Length;  
  1864.         long Tong = 0;  
  1865.         while (i < l)  
  1866.         {  
  1867.             a.Push(ToDec(Convert.ToString(x[i])));  
  1868.             i++;  
  1869.         }  
  1870.         while (a.Count != 0)  
  1871.         {  
  1872.             Tong = Tong + Convert.ToInt64(a.Pop()) * Pow(16, j);  
  1873.             j++;  
  1874.         }  
  1875.         z = Convert.ToString(Tong);  
  1876.         return z;  
  1877.     }  
  1878.  
  1879.     #endregion //Helperfunctions  
  1880.   
  1881.     /// <summary>  
  1882.     ///   
  1883.     /// </summary>  
  1884.     /// <param name="x"></param>  
  1885.     /// <param name="y"></param>  
  1886.     /// <returns></returns>  
  1887.     private static long Pow(long x, long y)  
  1888.     {  
  1889.         int i = 1;  
  1890.         long X = x;  
  1891.         if (y == 0)  
  1892.             return 1;  
  1893.         while (i < y)  
  1894.         {  
  1895.             x = x * X;  
  1896.             i++;  
  1897.         }  
  1898.         return x;  
  1899.     }  
  1900.   
  1901.     /// <summary>  
  1902.     ///   
  1903.     /// </summary>  
  1904.     /// <param name="x"></param>  
  1905.     /// <returns></returns>  
  1906.     private static string ToDec(string x)  
  1907.     {  
  1908.         switch (x)  
  1909.         {  
  1910.             case "A":  
  1911.                 return "10";  
  1912.             case "B":  
  1913.                 return "11";  
  1914.             case "C":  
  1915.                 return "12";  
  1916.             case "D":  
  1917.                 return "13";  
  1918.             case "E":  
  1919.                 return "14";  
  1920.             case "F":  
  1921.                 return "15";  
  1922.             default:  
  1923.                 return x;  
  1924.         }  
  1925.     }  
  1926.   
  1927.     /// <summary>  
  1928.     ///   
  1929.     /// </summary>  
  1930.     /// <param name="x"></param>  
  1931.     /// <returns></returns>  
  1932.     private static string ToHex(string x)  
  1933.     {  
  1934.         switch (x)  
  1935.         {  
  1936.             case "10":  
  1937.                 return "A";  
  1938.             case "11":  
  1939.                 return "B";  
  1940.             case "12":  
  1941.                 return "C";  
  1942.             case "13":  
  1943.                 return "D";  
  1944.             case "14":  
  1945.                 return "E";  
  1946.             case "15":  
  1947.                 return "F";  
  1948.             default:  
  1949.                 return x;  
  1950.         }  
  1951.     }  
  1952.   
  1953.     /// <summary>  
  1954.     /// 将16进制BYTE数组转换成16进制字符串  
  1955.     /// </summary>  
  1956.     /// <param name="bytes"></param>  
  1957.     /// <returns></returns>  
  1958.     public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "  
  1959.     {  
  1960.         string hexString = string.Empty;  
  1961.         if (bytes != null)  
  1962.         {  
  1963.             StringBuilder strB = new StringBuilder();  
  1964.   
  1965.             for (int i = 0; i < bytes.Length; i++)  
  1966.             {  
  1967.                 strB.Append(bytes[i].ToString("X2"));  
  1968.             }  
  1969.             hexString = strB.ToString();  
  1970.         }  
  1971.         return hexString;  
  1972.     }  
  1973. }  
  1974. #endregion  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值