码迷,mamicode.com
首页 > 其他好文 > 详细

编码转换

时间:2019-08-21 13:40:28      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:类型   catch   VID   col   create   builder   sys   style   编码方式   

C# Base64编码
class
Base64Helper { /// <summary> /// Base64加密,采用utf8编码方式加密 /// </summary> /// <param name="source">待加密的明文</param> /// <returns>加密后的字符串</returns> public static string Base64Encode(string source) { return Base64Encode(Encoding.UTF8, source); } /// <summary> /// Base64加密 /// </summary> /// <param name="encodeType">加密采用的编码方式</param> /// <param name="source">待加密的明文</param> /// <returns></returns> public static string Base64Encode(Encoding encodeType, string source) { string encode = string.Empty; byte[] bytes = encodeType.GetBytes(source); try { encode = Convert.ToBase64String(bytes); } catch { encode = source; } return encode; } /// <summary> /// Base64解密,采用utf8编码方式解密 /// </summary> /// <param name="result">待解密的密文</param> /// <returns>解密后的字符串</returns> public static string Base64Decode(string result) { return Base64Decode(Encoding.UTF8, result); } /// <summary> /// Base64解密 /// </summary> /// <param name="encodeType">解密采用的编码方式,注意和加密时采用的方式一致</param> /// <param name="result">待解密的密文</param> /// <returns>解密后的字符串</returns> public static string Base64Decode(Encoding encodeType, string result) { string decode = string.Empty; byte[] bytes = Convert.FromBase64String(result); try { decode = encodeType.GetString(bytes); } catch { decode = result; } return decode; } }
C# 文件与二进制流
///
<summary> /// 将文件转换为byte数组 /// </summary> /// <param name="path">文件地址</param> /// <returns>转换后的byte数组</returns> public static byte[] File2Bytes(string path) {   if (!System.IO.File.Exists(path))   {     return new byte[0];   }   FileInfo fi = new FileInfo(path);   byte[] buff = new byte[fi.Length];   FileStream fs = fi.OpenRead();   fs.Read(buff, 0, Convert.ToInt32(fs.Length));   fs.Close();   return buff; } /// <summary> /// 将byte数组转换为文件并保存到指定地址 /// </summary> /// <param name="buff">byte数组</param> /// <param name="savepath">保存地址</param> public static void Bytes2File(byte[] buff, string savepath) {   if (System.IO.File.Exists(savepath))   {     System.IO.File.Delete(savepath);   }   FileStream fs = new FileStream(savepath, FileMode.CreateNew);   BinaryWriter bw = new BinaryWriter(fs);   bw.Write(buff, 0, buff.Length);   bw.Close();   fs.Close(); }
C# MD5加密
public
static string MD5Encrypt(string strText) {   MD5 md5 = new MD5CryptoServiceProvider();   byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));   return System.Text.Encoding.Default.GetString(result); }

private static string GetMD5String(string sign) {   MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();   byte[] encryptedBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(sign));   StringBuilder sb = new StringBuilder();   for (int i = 0; i < encryptedBytes.Length; i++)   {     sb.AppendFormat("{0:x2}", encryptedBytes[i]);   }   return sb.ToString(); }
C# string和byte[]
string类型转成byte[]:
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); byte[]转成string: string str = System.Text.Encoding.Default.GetString ( byteArray );

编码转换

标签:类型   catch   VID   col   create   builder   sys   style   编码方式   

原文地址:https://www.cnblogs.com/az4215/p/11388172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!