C# string 和byte[]之间的转换
生活随笔
收集整理的這篇文章主要介紹了
C# string 和byte[]之间的转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
c#將string和byte數組之間互相轉換
?如下方法將字符串轉換為byte數組,使用System.Buffer.BlockCopy方法。
static byte[] GetBytes(string str) { byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return bytes; }將字節數組轉換為字符串,同樣是使用BlockCopy方法,這次是將字節數組復制到char數組中
static string GetString(byte[] bytes) { char[] chars = new char[bytes.Length / sizeof(char)]; System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length); return new string(chars); }//string 轉為byte數組
?byte[] array = Encoding.UTF8.GetBytes(content);
//將byte數組轉為string
string result = Encoding.UTF8.GetString(array);
轉載于:https://www.cnblogs.com/ting5/p/5044252.html
總結
以上是生活随笔為你收集整理的C# string 和byte[]之间的转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (计算机组成原理题目题型总结)第四章:指
- 下一篇: (数据库系统概论|王珊)第三章关系数据库