Removing trailing nulls from byte array in C#

前端 未结 10 2038
感情败类
感情败类 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:06

    @Factor Mystic,

    I think there is a shortest way:

    var data = new byte[] { 0x01, 0x02, 0x00, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00 };
    var new_data = data.TakeWhile((v, index) => data.Skip(index).Any(w => w != 0x00)).ToArray();
    

提交回复
热议问题