这篇文章主要讲解了“C# string怎么转换为几种不同编码的Byte[]”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C# string怎么转换为几种不同编码的Byte[]”吧!
①String类型转换为UTF8编码的Byte[]:
string strvalue ="00123aabbAA$%"; byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(strvalue);
②string类型转换为ASCII编码的Byte[]:
string strvalue ="00123aabbAA$%"; byte[] byteArray = System.Text.Encoding.ASCII.GetBytes (strvalue);
③string类型转换为GB18030编码的Byte[];
string strvalue ="00123aabbAA$%": byte[] byteArray = System.Text.Encoding.GetEncoding("GB18030").GetBytes (strvalue);
①UTF8编码的Byte[]转换为String类型:
byte[] byteArray = new byte[100]; String str = System.Text.Encoding.UTF8.GetString ( byteArray );
②ASCII编码的Byte[]转换为String类型:
byte[] byteArray = new byte[100]; String str = System.Text.Encoding.ASCII.GetString ( byteArray );
③GB18030编码的Byte[]转换为String类型:
byte[] byteArray = new byte[100]; String str = System.Text.Encoding.GetEncoding("GB18030").GetString ( byteArray );
下面对上面的类型转换
举例说明:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TRansfer { class Program { static void Main(string[] args) { string strvalue = "0aA$%#@!%^&*_+-=中国"; byte[] byteArray1 = System.Text.Encoding.UTF8.GetBytes(strvalue); byte[] byteArray2 = System.Text.Encoding.ASCII.GetBytes(strvalue); byte[] byteArray3 = System.Text.Encoding.GetEncoding("GB18030").GetBytes(strvalue); byte[] byteArray11 = new byte[100]; String str1 = System.Text.Encoding.UTF8.GetString(byteArray1); String str2 = System.Text.Encoding.ASCII.GetString(byteArray2); String str3 = System.Text.Encoding.GetEncoding("GB18030").GetString(byteArray3); string st1 = ""; foreach (byte b in byteArray1) { st1 += b.ToString(); } string st2 = ""; foreach (byte b in byteArray2) { st2 += b.ToString(); } string st3 = ""; foreach (byte b in byteArray3) { st3 += b.ToString(); } Console.WriteLine("{0} {1}", st1, str1); Console.WriteLine("{0} {1}", st2, str2); Console.WriteLine("{0} {1}", st3, str3); } } }
测试结果如下,对英文字符的翻译都是一致,各种不同的编码的差别主要体现在中文上面:
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; } }
/// <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(); }
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(); }
string类型转成byte[]:
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );
byte[]转成string:
string str = System.Text.Encoding.Default.GetString ( byteArray );
感谢各位的阅读,以上就是“C# string怎么转换为几种不同编码的Byte[]”的内容了,经过本文的学习后,相信大家对C# string怎么转换为几种不同编码的Byte[]这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。