您好,登錄后才能下訂單哦!
這篇“C#字節(jié)數(shù)組和字符串怎么相互轉(zhuǎn)換”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“C#字節(jié)數(shù)組和字符串怎么相互轉(zhuǎn)換”文章吧。
通過(guò)C#中的system.text.encoding獲取字符串的編碼可以有ASCII,DEFAULT,utf-8以及其他一些方式
對(duì)于英文而言這幾種所獲取的編碼是沒(méi)有太大區(qū)別的,而中文則大有不同,其中DEFAULT所采取的是GB2312
可以通過(guò)一下方式進(jìn)行確認(rèn),程序運(yùn)行后會(huì)發(fā)現(xiàn)bufOfGB和buf是相同的
string str = "hello,我的祖國(guó)"; byte[] bufOfGB = System.Text.Encoding.GetEncoding("gb2312").GetBytes(str); Array.ForEach(bufOfGB,m=>Console.WriteLine(m)); Console.WriteLine(System.Text.Encoding.Default); byte[] buf = System.Text.Encoding.Default.GetBytes(str); Array.ForEach(buf,m=>Console.WriteLine(m)); Console.WriteLine("-------------"); byte[] bufOfASCII = System.Text.Encoding.ASCII.GetBytes(str); Array.ForEach(bufOfASCII,m=>Console.WriteLine(m)); Console.WriteLine("-------------"); byte[] bufOfUTF = System.Text.Encoding.UTF8.GetBytes(str); Array.ForEach(bufOfUTF,m=>Console.WriteLine(m)); Console.WriteLine("-------------");
byte[] byteArray ={43,45,67,88,23,1f} string str = System.Text.Encoding.Default.GetString(byteArray);
#region BytesToBmp【函數(shù)】將字節(jié)數(shù)組轉(zhuǎn)化為圖像 /// <summary> /// 【函數(shù)】將字節(jié)數(shù)組轉(zhuǎn)化為圖像 /// </summary> /// <param name="buffer"></param> /// <returns></returns> public static Image BytesToBmp(byte[] buffer) { MemoryStream ms = new MemoryStream(buffer); ms.Position = 0; Image img = Image.FromStream(ms); ms.Close(); return img; } #endregion #region BmpToBytes【函數(shù)】將圖像轉(zhuǎn)化為字節(jié)數(shù)組 /// <summary> /// 【函數(shù)】將圖像轉(zhuǎn)化為字節(jié)數(shù)組 /// </summary> /// <param name="Bit"></param> /// <returns></returns> public static byte[] BmpToBytes(Bitmap Bit) { byte[] back = null; MemoryStream ms = new MemoryStream(); Bit.Save(ms, System.Drawing.Imaging.ImageFormat.Png); back = ms.GetBuffer(); return back; } #endregion
#region StringToBytes【函數(shù)】將字符串轉(zhuǎn)化為字節(jié)組 /// <summary> /// 【函數(shù)】將字符串轉(zhuǎn)化為字節(jié)組 /// </summary> /// <param name="str"></param> /// <returns></returns> public static byte[] StringToBytes(string str) { byte[] data = System.Text.Encoding.Default.GetBytes(str); } #endregion #region BytesToString【函數(shù)】將字節(jié)數(shù)組轉(zhuǎn)化為字符串 /// <summary> /// 【函數(shù)】將字節(jié)數(shù)組轉(zhuǎn)化為字符串 /// </summary> /// <param name="data"></param> /// <returns></returns> public static string BytesToString(byte[] data) { string str = System.Text.Encoding.Default.GetString(data); return str; } #endregion
#region BytesToInt【函數(shù)】byte數(shù)組轉(zhuǎn)int數(shù)組 /// <summary> /// 【函數(shù)】byte數(shù)組轉(zhuǎn)int數(shù)組 /// </summary> /// <param name="src">源byte數(shù)組</param> /// <param name="offset">起始位置</param> /// <returns></returns> public static int[] BytesToInt(byte[] src, int offset) { int[] values = new int[src.Length / 4]; for (int i = 0; i < src.Length / 4; i++) { int value = (int)((src[offset] & 0xFF) | ((src[offset + 1] & 0xFF) << 8) | ((src[offset + 2] & 0xFF) << 16) | ((src[offset + 3] & 0xFF) << 24)); values[i] = value; offset += 4; } return values; } #endregion #region IntToBytes【函數(shù)】int數(shù)組轉(zhuǎn)byte數(shù)組 /// <summary> /// 【函數(shù)】int數(shù)組轉(zhuǎn)byte數(shù)組 /// </summary> /// <param name="src">源int數(shù)組</param> /// <param name="offset">起始位置,一般為0</param> /// <returns>values</returns> public static byte[] IntToBytes(int[] src, int offset) { byte[] values = new byte[src.Length * 4]; for (int i = 0; i < src.Length; i++) { values[offset + 3] = (byte)((src[i] >> 24) & 0xFF); values[offset + 2] = (byte)((src[i] >> 16) & 0xFF); values[offset + 1] = (byte)((src[i] >> 8) & 0xFF); values[offset] = (byte)(src[i] & 0xFF); offset += 4; } return values; } #endregion
#region ObjectToBytes【函數(shù)】將一個(gè)object對(duì)象序列化,返回一個(gè)byte[] /// <summary> /// 【函數(shù)】將一個(gè)object對(duì)象序列化,返回一個(gè)byte[] /// </summary> /// <param name="obj">能序列化的對(duì)象</param> /// <returns></returns> public static byte[] ObjectToBytes(object obj) { using (MemoryStream ms = new MemoryStream()) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); return ms.GetBuffer(); } } #endregion #region BytesToObject【函數(shù)】將一個(gè)序列化后的byte[]數(shù)組還原 /// <summary> /// 【函數(shù)】將一個(gè)序列化后的byte[]數(shù)組還原 /// </summary> /// <param name="Bytes"></param> /// <returns></returns> public static object BytesToObject(byte[] Bytes) { using (MemoryStream ms = new MemoryStream(Bytes)) { IFormatter formatter = new BinaryFormatter(); return formatter.Deserialize(ms); } } #endregion
以上就是關(guān)于“C#字節(jié)數(shù)組和字符串怎么相互轉(zhuǎn)換”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。