首页 > 技术文章 > int[] convert byte[]

zhangchenliang 2016-11-01 18:25 原文

private void button_Click(object sender, RoutedEventArgs e)
        {
            byte[] bytes = this.ConvertIntArrayToByteArray(this.GetIntArray());

            int byteCount = bytes.Length;

            Console.WriteLine(byteCount.ToString());
        }

        private int[] GetIntArray()
        {
            return new int[5] { 1, 2, 3, 4, 5 };
        }

        private byte[] ConvertIntArrayToByteArray(int[] intArray)
        {
            List<byte> lst = new List<byte>();

            foreach (int item in intArray)
            {
                byte[] bytes = BitConverter.GetBytes(item);

                if(bytes != null)
                    lst.AddRange(bytes);
            }

            return lst.ToArray();
        }

 

推荐阅读