Removing trailing nulls from byte array in C#

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

    test this :

        private byte[] trimByte(byte[] input)
        {
            if (input.Length > 1)
            {
                int byteCounter = input.Length - 1;
                while (input[byteCounter] == 0x00)
                {
                    byteCounter--;
                }
                byte[] rv = new byte[(byteCounter + 1)];
                for (int byteCounter1 = 0; byteCounter1 < (byteCounter + 1); byteCounter1++)
                {
                    rv[byteCounter1] = input[byteCounter1];
                }
                return rv;
            }
    

提交回复
热议问题