138 lines
4.0 KiB
C#
138 lines
4.0 KiB
C#
/******************************************************************************
|
|
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
|
|
{
|
|
/// <summary>
|
|
/// 关于WEB项目的一些实用方法
|
|
/// </summary>
|
|
public static class EncodingHelper
|
|
{
|
|
|
|
#region Base64 Util
|
|
|
|
/// <summary>
|
|
/// 把字符串进行BASE64编码
|
|
/// </summary>
|
|
/// <param name="inputString">原字符串</param>
|
|
/// <param name="encodingName">编码格式名</param>
|
|
/// <returns>编码后的字符串</returns>
|
|
public static string ToBase64(string inputString, string encodingName) {
|
|
return Convert.ToBase64String(Encoding.GetEncoding(encodingName).GetBytes(inputString));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对BASE64字符串进行解码
|
|
/// </summary>
|
|
/// <param name="base64String">待解码的字符串</param>
|
|
/// <param name="encodingName">编码格式名</param>
|
|
/// <returns>解码后的字符串</returns>
|
|
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');
|
|
}
|
|
|
|
/// <summary>
|
|
/// 把字节数组转换成16进制表示的字符串
|
|
/// </summary>
|
|
/// <param name="sArray">字节数组</param>
|
|
/// <returns>16进制表示的字符串</returns>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 把16进制表示的字符串转换成字节数组
|
|
/// </summary>
|
|
/// <param name="hexString">进制表示的字符串</param>
|
|
/// <returns>字节数组</returns>
|
|
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
|
|
|
|
}
|
|
} |