Removing trailing nulls from byte array in C#

前端 未结 10 2060
感情败类
感情败类 2020-12-30 03:01

Ok, I am reading in dat files into a byte array. For some reason, the people who generate these files put about a half meg\'s worth of useless null bytes at the end of the

10条回答
  •  清歌不尽
    2020-12-30 03:26

    In my case LINQ approach never finished ^))) It's to slow to work with byte arrays!

    Guys, why won't you use Array.Copy() method?

        /// 
        /// Gets array of bytes from memory stream.
        /// 
        /// Memory stream.
        public static byte[] GetAllBytes(this MemoryStream stream)
        {
            byte[] result = new byte[stream.Length];
            Array.Copy(stream.GetBuffer(), result, stream.Length);
    
            return result;
        }
    

提交回复
热议问题