最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#字節(jié)數(shù)組(byte[])和字符串相互轉(zhuǎn)換方式

 更新時(shí)間:2023年02月28日 14:59:50   作者:IT技術(shù)猿猴  
這篇文章主要介紹了C#字節(jié)數(shù)組(byte[])和字符串相互轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

C#字節(jié)數(shù)組(byte[])和字符串相互轉(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);

C#字節(jié)數(shù)組向類型的轉(zhuǎn)化

字節(jié)數(shù)組與圖像

? ? ? #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

字節(jié)數(shù)組與字符串

? ? ? ? #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

字節(jié)數(shù)組與整數(shù)

? ?#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

字節(jié)數(shù)組與Object

? ? ? ? #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

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#自定義WPF中Slider的Autotooltip模板

    C#自定義WPF中Slider的Autotooltip模板

    這篇文章介紹了C#自定義WPF中Slider的Autotooltip模板的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • 微信跳一跳自動(dòng)腳本C#代碼實(shí)現(xiàn)

    微信跳一跳自動(dòng)腳本C#代碼實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了微信跳一跳自動(dòng)腳本C#代碼實(shí)現(xiàn)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C# DialogResult用法案例詳解

    C# DialogResult用法案例詳解

    這篇文章主要介紹了C# DialogResult用法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C#使用Stack<T>進(jìn)行堆棧設(shè)計(jì)的實(shí)現(xiàn)

    C#使用Stack<T>進(jìn)行堆棧設(shè)計(jì)的實(shí)現(xiàn)

    堆棧代表了一個(gè)后進(jìn)先出的對(duì)象集合,當(dāng)您需要對(duì)各項(xiàng)進(jìn)行后進(jìn)先出的訪問(wèn)時(shí),則使用堆棧,本文主要介紹了C#使用Stack<T>類進(jìn)行堆棧設(shè)計(jì)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),感興趣的可以了解一下
    2024-03-03
  • C#中常使用進(jìn)度條的代碼

    C#中常使用進(jìn)度條的代碼

    C#中常使用進(jìn)度條的代碼...
    2007-03-03
  • unity shader實(shí)現(xiàn)玻璃折射效果

    unity shader實(shí)現(xiàn)玻璃折射效果

    這篇文章主要為大家詳細(xì)介紹了unity shader實(shí)現(xiàn)玻璃折射效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C#實(shí)現(xiàn)TreeView節(jié)點(diǎn)拖拽的方法

    C#實(shí)現(xiàn)TreeView節(jié)點(diǎn)拖拽的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)TreeView節(jié)點(diǎn)拖拽的方法,涉及C#針對(duì)TreeView節(jié)點(diǎn)的動(dòng)態(tài)添加及移除技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • c# 應(yīng)用NPOI獲取Excel中的圖片,保存至本地的算法

    c# 應(yīng)用NPOI獲取Excel中的圖片,保存至本地的算法

    本文主要介紹了c# 應(yīng)用NPOI獲取Excel中的圖片,保存至本地的算法。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-02-02
  • C#基礎(chǔ)教程之類class與結(jié)構(gòu)struct的區(qū)別

    C#基礎(chǔ)教程之類class與結(jié)構(gòu)struct的區(qū)別

    struct是值類型,創(chuàng)建一個(gè)struct類型的實(shí)例被分配在棧上,class是引用類型,創(chuàng)建一個(gè)class類型實(shí)例被分配在托管堆上,下面這篇文章主要給大家介紹了關(guān)于C#基礎(chǔ)教程之類class與結(jié)構(gòu)struct區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • C#生成本地配置文件的實(shí)現(xiàn)示例

    C#生成本地配置文件的實(shí)現(xiàn)示例

    本文將介紹如何使用C#語(yǔ)言生成本地配置文件,以便為應(yīng)用程序提供靈活的配置選項(xiàng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01

最新評(píng)論

大名县| 石城县| 鸡东县| 慈利县| 汝城县| 宝坻区| 康保县| 明溪县| 冕宁县| 巢湖市| 金堂县| 承德县| 郧西县| 三河市| 保定市| 繁峙县| 黎川县| 麦盖提县| 大名县| 梓潼县| 青海省| 金坛市| 大港区| 张家界市| 八宿县| 邹城市| 平顶山市| 九江县| 墨江| 马山县| 寿阳县| 新绛县| 新建县| 治多县| 监利县| 方城县| 横峰县| 慈利县| 高雄市| 牙克石市| 孙吴县|