/// <summary>
/// GB2312编码
/// </summary>
/// <param name="str">待编码字符</param>
/// <returns>编码结果</returns>
1 public static string GB2312(string str) 2 { 3 StringBuilder sb = new StringBuilder(); 4 //GB2321的编码方式 5 byte[] byStr = System.Text.Encoding.GetEncoding ("GB2312").GetBytes(str); 6 for (int i = 0; i < byStr.Length; i++) 7 { 8 //转换为16进制方式 可选2,8,10,16进制 9 sb.Append(@"%" + Convert.ToString(byStr[i], 16)); 10 } 11 return (sb.ToString()); 12 }
/// <summary>
/// GB2312解码
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
1 public static string DeGB2312(string str) 2 { 3 byte[] bytes = new byte[str.Split('%').Length ]; 4 int i=0; 5 foreach (var item in str.Split('%')) 6 { 7 if (item !="") 8 { 9 //转换为16进制的字节 10 bytes[i] = Convert.ToByte(item,16); 11 i++; 12 } 13 14 } 15 //GB2312的解码 16 return Encoding.GetEncoding ("GB2312").GetString(bytes); 17 }
/// <summary>
/// UTF8编码
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
1 public static string EnUTF8(string str) 2 { 3 StringBuilder sb = new StringBuilder(); 4 byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); 5 for (int i = 0; i < byStr.Length; i++) 6 { 7 sb.Append(@"%" + Convert.ToString(byStr[i], 16)); 8 } 9 10 return (sb.ToString()); 11 }
/// <summary>
/// UTF8解码
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
1 public static string DeUTF8(string str) 2 { 3 byte[] bytes = new byte[str.Split('%').Length ]; 4 int i=0; 5 foreach (var item in str.Split('%')) 6 { 7 if (item !="") 8 { 9 bytes[i] = Convert.ToByte(item,16); 10 i++; 11 } 12 13 } 14 return Encoding.UTF8.GetString(bytes); 15 }
///C# 32位md5
/// <summary>
/// 获得32位的MD5加密
/// </summary>
/// <param name="str">加密字符串</param>
/// <returns>返回值</returns>
1 public static string GetMD532(string str) 2 { 3 MD5 md5 = MD5.Create(); 4 byte[] d = md5.ComputeHash(Encoding.Default.GetBytes(str)); 5 return BitConverter.ToString(d).Replace("-", "").ToLower(); 6 7 }