Removing trailing nulls from byte array in C#

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

    There is always a LINQ answer

    byte[] data = new byte[] { 0x01, 0x02, 0x00, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00 };
    bool data_found = false;
    byte[] new_data = data.Reverse().SkipWhile(point =>
    {
      if (data_found) return false;
      if (point == 0x00) return true; else { data_found = true; return false; }
    }).Reverse().ToArray();
    

提交回复
热议问题