C# string轉(zhuǎn)換為幾種不同編碼的Byte[]的問(wèn)題解讀
C# string轉(zhuǎn)換為幾種不同編碼的Byte[]
1.string–>Byte[]轉(zhuǎn)換
①String類型轉(zhuǎn)換為UTF8編碼的Byte[]:
string strvalue ="00123aabbAA$%"; byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(strvalue);
②string類型轉(zhuǎn)換為ASCII編碼的Byte[]:
string strvalue ="00123aabbAA$%"; byte[] byteArray = System.Text.Encoding.ASCII.GetBytes (strvalue);
③string類型轉(zhuǎn)換為GB18030編碼的Byte[];
string strvalue ="00123aabbAA$%": byte[] byteArray = System.Text.Encoding.GetEncoding("GB18030").GetBytes (strvalue);
2.Byte[]–>string轉(zhuǎn)換
①UTF8編碼的Byte[]轉(zhuǎn)換為String類型:
byte[] byteArray = new byte[100]; String str = System.Text.Encoding.UTF8.GetString ( byteArray );
②ASCII編碼的Byte[]轉(zhuǎn)換為String類型:
byte[] byteArray = new byte[100]; String str = System.Text.Encoding.ASCII.GetString ( byteArray );
③GB18030編碼的Byte[]轉(zhuǎn)換為String類型:
byte[] byteArray = new byte[100]; String str = System.Text.Encoding.GetEncoding("GB18030").GetString ( byteArray );
下面對(duì)上面的類型轉(zhuǎn)換
舉例說(shuō)明:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TRansfer { ? ? class Program ? ? { ? ? ? ? static void Main(string[] args) ? ? ? ?{ ? ? ? ? ? ? string strvalue = "0aA$%#@!%^&*_+-=中國(guó)"; ? ? ? ? ? ? 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); ? ? ? ? } ? ? } }
測(cè)試結(jié)果如下,對(duì)英文字符的翻譯都是一致,各種不同的編碼的差別主要體現(xiàn)在中文上面:
C#分享幾種常用的編碼轉(zhuǎn)換,base64、MD5、string
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">解密采用的編碼方式,注意和加密時(shí)采用的方式一致</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# 文件與二進(jìn)制流
? ? /// <summary> ? ? /// 將文件轉(zhuǎn)換為byte數(shù)組 ? ? /// </summary> ? ? /// <param name="path">文件地址</param> ? ? /// <returns>轉(zhuǎn)換后的byte數(shù)組</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數(shù)組轉(zhuǎn)換為文件并保存到指定地址 ? ? /// </summary> ? ? /// <param name="buff">byte數(shù)組</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類型轉(zhuǎn)成byte[]:
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );
byte[]轉(zhuǎn)成string:
string str = System.Text.Encoding.Default.GetString ( byteArray );
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#簡(jiǎn)單實(shí)現(xiàn)表達(dá)式目錄樹(shù)(Expression)
表達(dá)式目錄樹(shù)以數(shù)據(jù)形式表示語(yǔ)言級(jí)別代碼。數(shù)據(jù)存儲(chǔ)在樹(shù)形結(jié)構(gòu)中。表達(dá)式目錄樹(shù)中的每個(gè)節(jié)點(diǎn)都表示一個(gè)表達(dá)式。這篇文章給大家介紹C#簡(jiǎn)單實(shí)現(xiàn)表達(dá)式目錄樹(shù)(Expression),需要的朋友參考下吧2017-11-11淺析C#數(shù)據(jù)類型轉(zhuǎn)換的幾種形式
本篇文章是對(duì)C#中數(shù)據(jù)類型轉(zhuǎn)換的幾種形式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07C# HttpClient Post參數(shù)同時(shí)上傳文件的實(shí)現(xiàn)
這篇文章主要介紹了C# HttpClient Post參數(shù)同時(shí)上傳文件的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06