SoFunction
Updated on 2025-03-06

How to convert C# byte array (byte[]) and strings to each other

C# byte array (byte[]) and strings are converted to each other

Through the encoding of strings in C#, you can have ASCII, DEFAULT, utf-8 and some other ways

For English, there is not much difference between the encodings obtained by these types of codes, while in Chinese, there are very different ones. Among them, DEFAULT adopts GB2312

You can confirm it in the following way. After the program runs, you will find that bufOfGB and buf are the same.

                string str = "hello, my motherland";
                byte[] bufOfGB = ("gb2312").GetBytes(str);
                (bufOfGB,m=>(m));
                ();
                byte[] buf = (str);
                (buf,m=>(m));
                ("-------------");
                byte[] bufOfASCII = (str);
                (bufOfASCII,m=>(m));
                ("-------------");
                byte[] bufOfUTF = .(str);
                (bufOfUTF,m=>(m));
                ("-------------");
byte[] byteArray ={43,45,67,88,23,1f}
string str = (byteArray);

Conversion of C# byte array to type

Byte array and image

      #region BytesToBmp【Function】Convert byte array into image        /// <summary>
        /// 【Function】Convert byte array into image        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static Image BytesToBmp(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer);
             = 0;
            Image img = (ms);
            ();
            return img;
        }
        #endregion
 
        #region BmpToBytes【Function】Converts images into byte arrays        /// <summary>
        /// 【Function】Convert image into byte array        /// </summary>
        /// <param name="Bit"></param>
        /// <returns></returns>
        public static byte[] BmpToBytes(Bitmap Bit)
        {
            byte[] back = null;
            MemoryStream ms = new MemoryStream();
            (ms, );
            back = ();
            return back;
        }
        #endregion

Byte array and string

        #region StringToBytes【Function】Convert strings into byte groups        /// <summary>
        /// 【Function】Convert strings into byte groups        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] StringToBytes(string str)
        {
            byte[] data = (str);
        }
        #endregion
 
        #region BytesToString【Function】Convert byte array into string        /// <summary>
        /// 【Function】Convert byte array into string        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string BytesToString(byte[] data)
        {
            string str = (data);
            return str;
        }
        #endregion

Byte array and integer

   #region BytesToInt【Function】byte array to int array        /// <summary>  
        /// 【Function】byte array to int array        /// </summary>  
        /// <param name="src">source byte array</param>        /// <param name="offset">First position</param>        /// &lt;returns&gt;&lt;/returns&gt;  
        public static int[] BytesToInt(byte[] src, int offset)
        {
            int[] values = new int[ / 4];
            for (int i = 0; i &lt;  / 4; i++)
            {
                int value = (int)((src[offset] &amp; 0xFF)
                        | ((src[offset + 1] &amp; 0xFF) &lt;&lt; 8)
                        | ((src[offset + 2] &amp; 0xFF) &lt;&lt; 16)
                        | ((src[offset + 3] &amp; 0xFF) &lt;&lt; 24));
                values[i] = value;
                offset += 4;
            }
            return values;
        }
        #endregion
 
        #region IntToBytes【Function】int array to byte array        /// &lt;summary&gt;  
        /// [Function] to int array to byte array        /// &lt;/summary&gt;  
        /// <param name="src">Source int array</param>        /// <param name="offset">The starting position is generally 0</param>        /// &lt;returns&gt;values&lt;/returns&gt;  
        public static byte[] IntToBytes(int[] src, int offset)
        {
            byte[] values = new byte[ * 4];
            for (int i = 0; i &lt; ; i++)
            {
 
                values[offset + 3] = (byte)((src[i] &gt;&gt; 24) &amp; 0xFF);
                values[offset + 2] = (byte)((src[i] &gt;&gt; 16) &amp; 0xFF);
                values[offset + 1] = (byte)((src[i] &gt;&gt; 8) &amp; 0xFF);
                values[offset] = (byte)(src[i] &amp; 0xFF);
                offset += 4;
            }
            return values;
        }
        #endregion

Byte array and Object

        #region ObjectToBytes【Function】Serializes an object object and returns a byte[]        /// &lt;summary&gt; 
        /// [Function] Serialize an object object and return a byte[]        /// &lt;/summary&gt; 
        /// <param name="obj">Objects that can be serialized</param>        /// &lt;returns&gt;&lt;/returns&gt; 
        public static byte[] ObjectToBytes(object obj)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter();
                (ms, obj);
                return ();
            }
        }
        #endregion
 
        #region BytesToObject【Function】Restore a serialized byte[] array        /// &lt;summary&gt; 
        /// [Function] Restore a serialized byte[] array        /// &lt;/summary&gt;
        /// &lt;param name="Bytes"&gt;&lt;/param&gt;         
        /// &lt;returns&gt;&lt;/returns&gt; 
        public static object BytesToObject(byte[] Bytes)
        {
            using (MemoryStream ms = new MemoryStream(Bytes))
            {
                IFormatter formatter = new BinaryFormatter();
                return (ms);
            }
        }
        #endregion

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.