下一站天后

今朝的容颜老于昨晚

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
using System;
using System.Web.UI;
using System.Text;
using System.Text.RegularExpressions;
using   System.Security;   
using   System.Security.Cryptography;  
namespace bchataspx.tools
{
    
/// <summary>
    
/// Utils 的摘要说明。
    
/// </summary>

    public class Utils
    
{
        
public Utils()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }


        
public static void MessageBox(string msg)
        
{
            StringBuilder sb 
= new StringBuilder();
            sb.Append(
"<Script language=\"javascript\" defer>\r\n");
            sb.Append(
"alert(\""+msg+"\");\r\n");
            sb.Append(
"</script>\r\n");
 
            System.Web.HttpContext.Current.Response.Write(sb.ToString());
             
        }

        
public static void MessageBox(string msg,System.Web.UI.Page p)
        
{
            StringBuilder sb 
= new StringBuilder();
            sb.Append(
"<Script language=\"javascript\" defer>\r\n");
            sb.Append(
"alert(\""+msg+"\");\r\n");
            sb.Append(
"</script>\r\n");
  
            p.RegisterStartupScript(
"jsScript",sb.ToString());
        }



        
public static void MessageBox(string msg,string url)
        
{
            StringBuilder sb 
= new StringBuilder();
            sb.Append(
"<Script language=\"javascript\" defer>\r\n");
            sb.Append(
"alert(\""+msg+"\");\r\n");
            sb.Append(
"window.location.href=\""+url+"\";\r\n");

            sb.Append(
"</script>\r\n");

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
        }

        
public static void MessageBoxUrl(string ms,string url)
        
{
            
string sb="<script language='javascript' defer>\r\n";
            sb
+="alert('"+ms+"');\r\n";
            sb
+="window.location='"+url+"';\r\n";
            sb
+="</script>\r\n";
            System.Web.HttpContext.Current.Response.Write(sb);
        }


        
public static string SqlFilter(string sql)
        
{
            
return sql.Replace("'","").Replace("--","");
        }


        
/// <summary>
        
/// 得到URL参数值
        
/// 如果不存在返回 ""
        
/// </summary>
        
/// <param name="urlparameters"></param>
        
/// <returns></returns>

        public static string GetQueryString(string urlparameters)
        
{
            
string value = System.Web.HttpContext.Current.Request.QueryString[urlparameters];
            
return value == null ? "" : value.Replace("'""");
        }


        
public static string GetPostString(string p)
        
{
            
string v =  System.Web.HttpContext.Current.Request.Form[p];
            
return v == null ? "" : v.Replace("'""");
        }


        
public static int GetPostInt(string p)
        
{
            
string v = GetPostString(p);
            
return StrToInt(v,0);
        }

        
        
/// <summary>
        
/// 得到URL参数值
        
/// 整数类型
        
/// </summary>
        
/// <param name="urlparameters"></param>
        
/// <returns>
        
/// 若不存在 或错误,则返回0</returns>

        public static int GetQueryInt(string urlparameters)
        
{
            
string value = GetQueryString(urlparameters);

            
return StrToInt(value,0);
        }


        
/// <summary>
        
/// 将对象转换为Int32类型
        
/// </summary>
        
/// <param name="strValue">要转换的字符串</param>
        
/// <param name="defValue">缺省值</param>
        
/// <returns>转换后的int类型结果</returns>

        public static int StrToInt(object Expression, int defValue)
        
{

            
if (Expression != null)
            
{
                
string str = Expression.ToString();
                
if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*$"))
                
{
                    
if ((str.Length < 10|| (str.Length == 10 && str[0== '1'|| (str.Length == 11 && str[0== '-' && str[1== '1'))
                    
{
                        
return Convert.ToInt32(str);
                    }

                }

            }

            
return defValue;
        }



        
public static string MD5(string inputString)   
        
{   
//            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
//            byte[] encryptedBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(inputString));
//            StringBuilder sb = new StringBuilder();
//            for (int i = 0; i < encryptedBytes.Length; i++)
//            {
//                sb.AppendFormat("{0:x2}", encryptedBytes[i]);
//            }
//            return sb.ToString();


            
byte[] b = System.Text.Encoding.GetEncoding("gb2312").GetBytes(inputString);
            System.Security.Cryptography.MD5 md5 
= System.Security.Cryptography.MD5.Create();
            
byte[] d = md5.ComputeHash(b);
            
string r = "";
            
for (int i = 0; i < d.Length; i++)
                r 
+= d[i].ToString("x").PadLeft(2'0');
            
return r;
        }
   

        
加密MD5

        
public static string ClearHtml(string strhtml)
        
{
            
if(strhtml==null)
                
return "";

            
string stroutput = strhtml;
            Regex regex 
= new Regex(@"<[^>]+>|</[^>]+>");

            
try
            
{
                stroutput 
= regex.Replace(stroutput, "");
            }

            
catch(Exception ex)
            
{
                
string m = ex.Message;
            }

            
return stroutput;

        }


        
/// <summary>
        
/// 截取去年HTML标记
        
/// </summary>
        
/// <param name="str">截取对象</param>
        
/// <param name="length">截取长度</param>
        
/// <param name="addChar">未尾添加的字符如…</param>
        
/// <returns></returns>

        public static string StringCutWithNoHtml(object str,int length,string addChar)
        
{
            
if(str == null || str.ToString() =="")
                
return "";
             
            
return StringCut(ClearHtml(str.ToString()),length,addChar);
        }


        
/// <summary>
        
/// 按长度截取字符串
        
/// </summary>
        
/// <param name="strInput">输入字符串</param>
        
/// <param name="intLen">截取长度</param>
        
/// <param name="addChar">未尾添加的字符如…</param>
        
/// <returns></returns>

        public static string StringCut(string strInput ,int intLen,string addChar)
        
{
            strInput 
= strInput.Trim() ;
            
int byteLen = Encoding.GetEncoding("gb2312").GetByteCount( strInput ) ;
            
if ( byteLen > intLen )
            
{
                
string resultStr = "" ;
                
for ( int i = 0 ; i < strInput.Length ; i++ )
                
{
                    
if ( Encoding.GetEncoding("gb2312").GetByteCount( resultStr ) < intLen - 2
                    
{
                        resultStr 
+= strInput.Substring( i , 1 ) ;
                    }

                    
else
                    
{
                        
break ;
                    }

                }

                
return resultStr + addChar ;
            }

            
else
            
{
                
return strInput ;
            }

        }



        
//返回给定字符串的首字母
        public static  String GetSentenceSpell(String IndexTxt)
        
{
            String _Temp
=null;
            
for(int i=0;i<IndexTxt.Length;i++)
                _Temp 
= _Temp + GetOneSpell( IndexTxt.Substring(i,1) );
            
return _Temp;
        }

        
//得到单个字符的首字母
        public static String GetOneSpell(String OneIndexTxt)
        
{
            
if(Convert.ToChar(OneIndexTxt)>=0 && Convert.ToChar(OneIndexTxt)<256)
                
return OneIndexTxt;
            
else
            
{
                Encoding gb2312 
= Encoding.GetEncoding("gb2312");
                
byte[] unicodeBytes = Encoding.Unicode.GetBytes(OneIndexTxt);
                
byte[] gb2312Bytes = Encoding.Convert(Encoding.Unicode, gb2312, unicodeBytes);
                
return GetX(Convert.ToInt32(
                    String.Format(
"{0:D2}",Convert.ToInt16(gb2312Bytes[0])-160)
                    
+ String.Format("{0:D2}",Convert.ToInt16(gb2312Bytes[1])-160)
                    ));
            }

 
        }

        
//根据区位得到首字母
        private static String GetX(int GBCode)
        
{    
            
if(GBCode >= 1601 && GBCode < 1637)return "A";
            
if(GBCode >= 1637 && GBCode < 1833)return "B";
            
if(GBCode >= 1833 && GBCode < 2078)return "C";
            
if(GBCode >= 2078 && GBCode < 2274)return "D";
            
if(GBCode >= 2274 && GBCode < 2302)return "E";
            
if(GBCode >= 2302 && GBCode < 2433)return "F";
            
if(GBCode >= 2433 && GBCode < 2594)return "G";
            
if(GBCode >= 2594 && GBCode < 2787)return "H";
            
if(GBCode >= 2787 && GBCode < 3106)return "J";
            
if(GBCode >= 3106 && GBCode < 3212)return "K";
            
if(GBCode >= 3212 && GBCode < 3472)return "L";
            
if(GBCode >= 3472 && GBCode < 3635)return "M";
            
if(GBCode >= 3635 && GBCode < 3722)return "N";
            
if(GBCode >= 3722 && GBCode < 3730)return "O";
            
if(GBCode >= 3730 && GBCode < 3858)return "P";
            
if(GBCode >= 3858 && GBCode < 4027)return "Q";
            
if(GBCode >= 4027 && GBCode < 4086)return "R";
            
if(GBCode >= 4086 && GBCode < 4390)return "S";
            
if(GBCode >= 4390 && GBCode < 4558)return "T";
            
if(GBCode >= 4558 && GBCode < 4684)return "W";
            
if(GBCode >= 4684 && GBCode < 4925)return "X";
            
if(GBCode >= 4925 && GBCode < 5249)return "Y";
            
if(GBCode >= 5249 && GBCode <= 5589)return "Z";
            
if(GBCode >= 5601 && GBCode <= 8794)
            
{
                String CodeData 
= "cjwgnspgcenegypbtwxzdxykygtpjnmjqmbsgzscyjsyyfpggbzgydywjkgaljswkbjqhyjwpdzlsgmr"
                    
+ "ybywwccgznkydgttngjeyekzydcjnmcylqlypyqbqrpzslwbdgkjfyxjwcltbncxjjjjcxdtqsqzycdxxhgckbphffss"
                    
+ "pybgmxjbbyglbhlssmzmpjhsojnghdzcdklgjhsgqzhxqgkezzwymcscjnyetxadzpmdssmzjjqjyzcjjfwqjbdzbjgd"
                    
+ "nzcbwhgxhqkmwfbpbqdtjjzkqhylcgxfptyjyyzpsjlfchmqshgmmxsxjpkdcmbbqbefsjwhwwgckpylqbgldlcctnma"
                    
+ "eddksjngkcsgxlhzaybdbtsdkdylhgymylcxpycjndqjwxqxfyyfjlejbzrwccqhqcsbzkymgplbmcrqcflnymyqmsqt"
                    
+ "rbcjthztqfrxchxmcjcjlxqgjmshzkbswxemdlckfsydsglycjjssjnqbjctyhbftdcyjdgwyghqfrxwckqkxebpdjpx"
                    
+ "jqsrmebwgjlbjslyysmdxlclqkxlhtjrjjmbjhxhwywcbhtrxxglhjhfbmgykldyxzpplggpmtcbbajjzyljtyanjgbj"
                    
+ "flqgdzyqcaxbkclecjsznslyzhlxlzcghbxzhznytdsbcjkdlzayffydlabbgqszkggldndnyskjshdlxxbcghxyggdj"
                    
+ "mmzngmmccgwzszxsjbznmlzdthcqydbdllscddnlkjyhjsycjlkohqasdhnhcsgaehdaashtcplcpqybsdmpjlpcjaql"
                    
+ "cdhjjasprchngjnlhlyyqyhwzpnccgwwmzffjqqqqxxaclbhkdjxdgmmydjxzllsygxgkjrywzwyclzmcsjzldbndcfc"
                    
+ "xyhlschycjqppqagmnyxpfrkssbjlyxyjjglnscmhcwwmnzjjlhmhchsyppttxrycsxbyhcsmxjsxnbwgpxxtaybgajc"
                    
+ "xlypdccwqocwkccsbnhcpdyznbcyytyckskybsqkkytqqxfcwchcwkelcqbsqyjqcclmthsywhmktlkjlychwheqjhtj"
                    
+ "hppqpqscfymmcmgbmhglgsllysdllljpchmjhwljcyhzjxhdxjlhxrswlwzjcbxmhzqxsdzpmgfcsglsdymjshxpjxom"
                    
+ "yqknmyblrthbcftpmgyxlchlhlzylxgsssscclsldclepbhshxyyfhbmgdfycnjqwlqhjjcywjztejjdhfblqxtqkwhd"
                    
+ "chqxagtlxljxmsljhdzkzjecxjcjnmbbjcsfywkbjzghysdcpqyrsljpclpwxsdwejbjcbcnaytmgmbapclyqbclzxcb"
                    
+ "nmsggfnzjjbzsfqyndxhpcqkzczwalsbccjxpozgwkybsgxfcfcdkhjbstlqfsgdslqwzkxtmhsbgzhjcrglyjbpmljs"
                    
+ "xlcjqqhzmjczydjwbmjklddpmjegxyhylxhlqyqhkycwcjmyhxnatjhyccxzpcqlbzwwwtwbqcmlbmynjcccxbbsnzzl"
                    
+ "jpljxyztzlgcldcklyrzzgqtgjhhgjljaxfgfjzslcfdqzlclgjdjcsnclljpjqdcclcjxmyzftsxgcgsbrzxjqqcczh"
                    
+ "gyjdjqqlzxjyldlbcyamcstylbdjbyregklzdzhldszchznwczcllwjqjjjkdgjcolbbzppglghtgzcygezmycnqcycy"
                    
+ "hbhgxkamtxyxnbskyzzgjzlqjdfcjxdygjqjjpmgwgjjjpkjsbgbmmcjssclpqpdxcdyykypcjddyygywchjrtgcnyql"
                    
+ "dkljczzgzccjgdyksgpzmdlcphnjafyzdjcnmwescsglbtzcgmsdllyxqsxsbljsbbsgghfjlwpmzjnlyywdqshzxtyy"
                    
+ "whmcyhywdbxbtlmswyyfsbjcbdxxlhjhfpsxzqhfzmqcztqcxzxrdkdjhnnyzqqfnqdmmgnydxmjgdhcdycbffallztd"
                    
+ "ltfkmxqzdngeqdbdczjdxbzgsqqddjcmbkxffxmkdmcsychzcmljdjynhprsjmkmpcklgdbqtfzswtfgglyplljzhgjj"
                    
+ "gypzltcsmcnbtjbhfkdhbyzgkpbbymtdlsxsbnpdkleycjnycdykzddhqgsdzsctarlltkzlgecllkjljjaqnbdggghf"
                    
+ "jtzqjsecshalqfmmgjnlyjbbtmlycxdcjpldlpcqdhsycbzsckbzmsljflhrbjsnbrgjhxpdgdjybzgdlgcsezgxlblg"
                    
+ "yxtwmabchecmwyjyzlljjshlgndjlslygkdzpzxjyyzlpcxszfgwyydlyhcljscmbjhblyjlycblydpdqysxktbytdkd"
                    
+ "xjypcnrjmfdjgklccjbctbjddbblblcdqrppxjcglzcshltoljnmdddlngkaqakgjgyhheznmshrphqqjchgmfprxcjg"
                    
+ "dychghlyrzqlcngjnzsqdkqjymszswlcfqjqxgbggxmdjwlmcrnfkkfsyyljbmqammmycctbshcptxxzzsmphfshmclm"
                    
+ "ldjfyqxsdyjdjjzzhqpdszglssjbckbxyqzjsgpsxjzqznqtbdkwxjkhhgflbcsmdldgdzdblzkycqnncsybzbfglzzx"
                    
+ "swmsccmqnjqsbdqsjtxxmbldxcclzshzcxrqjgjylxzfjphymzqqydfqjjlcznzjcdgzygcdxmzysctlkphtxhtlbjxj"
                    
+ "lxscdqccbbqjfqzfsltjbtkqbsxjjljchczdbzjdczjccprnlqcgpfczlclcxzdmxmphgsgzgszzqjxlwtjpfsyaslcj"
                    
+ "btckwcwmytcsjjljcqlwzmalbxyfbpnlschtgjwejjxxglljstgshjqlzfkcgnndszfdeqfhbsaqdgylbxmmygszldyd"
                    
+ "jmjjrgbjgkgdhgkblgkbdmbylxwcxyttybkmrjjzxqjbhlmhmjjzmqasldcyxyqdlqcafywyxqhz";
                String _gbcode 
= GBCode.ToString();
                
int pos = (Convert.ToInt16(_gbcode.Substring(0,2))-56)*94+Convert.ToInt16(_gbcode.Substring(_gbcode.Length-2,2));
                
return CodeData.Substring(pos-1,1);
            }
 
            
return " ";
        }

    }

}

posted on 2008-12-23 11:57  孙雅玲  阅读(214)  评论(0编辑  收藏  举报