/****************************************************************************** Copyright 2005-2007 R2@DevFx.NET DevFx.NET is free software; you can redistribute it and/or modify it under the terms of the Lesser GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. DevFx.NET is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public License for more details. You should have received a copy of the Lesser GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA /*******************************************************************************/ using System; using System.Collections.Specialized; using System.IO; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Collections.Generic; using System.Text.RegularExpressions; namespace Lskj.DevFx { /// /// 关于WEB项目的一些实用方法 /// public static class EncodingHelper { #region Base64 Util /// /// 把字符串进行BASE64编码 /// /// 原字符串 /// 编码格式名 /// 编码后的字符串 public static string ToBase64(string inputString, string encodingName) { return Convert.ToBase64String(Encoding.GetEncoding(encodingName).GetBytes(inputString)); } /// /// 对BASE64字符串进行解码 /// /// 待解码的字符串 /// 编码格式名 /// 解码后的字符串 public static string FromBase64(string base64String, string encodingName) { return Encoding.GetEncoding(encodingName).GetString(Convert.FromBase64String(base64String)); } #endregion Base64 Util #region Hex Util private static readonly char[] hexValues = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; private static int ConvertHexDigit(char val) { if(val <= '9') { return (val - '0'); } if(val >= 'a') { return ((val - 'a') + '\n'); } return ((val - 'A') + '\n'); } /// /// 把字节数组转换成16进制表示的字符串 /// /// 字节数组 /// 16进制表示的字符串 public static string ToHexString(byte[] sArray) { if(sArray == null) { return null; } char[] chArray = new char[sArray.Length * 2]; int i = 0; int k = 0; while(i < sArray.Length) { int t = (sArray[i] & 240) >> 4; chArray[k++] = hexValues[t]; t = sArray[i] & 15; chArray[k++] = hexValues[t]; i++; } return new string(chArray); } /// /// 把16进制表示的字符串转换成字节数组 /// /// 进制表示的字符串 /// 字节数组 public static byte[] FromHexString(string hexString) { byte[] buffer; if(hexString == null) { throw new ArgumentNullException("hexString"); } bool flag = false; int i = 0; int hexLength = hexString.Length; if(((hexLength >= 2) && (hexString[0] == '0')) && ((hexString[1] == 'x') || (hexString[1] == 'X'))) { hexLength = hexString.Length - 2; i = 2; } if(((hexLength % 2) != 0) && ((hexLength % 3) != 2)) { throw new ArgumentException("无效的16进制字符串格式"); } if((hexLength >= 3) && (hexString[i + 2] == ' ')) { flag = true; buffer = new byte[(hexLength / 3) + 1]; } else { buffer = new byte[hexLength / 2]; } for(int k = 0; i < hexString.Length; k++) { int h = ConvertHexDigit(hexString[i]); int l = ConvertHexDigit(hexString[i + 1]); buffer[k] = (byte)(l | (h << 4)); if(flag) { i++; } i += 2; } return buffer; } #endregion Hex Util } }