Has more bits for array of bytes

  • Thread starter Thread starter sasha
  • Start date Start date
S

sasha

I have byte[1024] and I am parsing it for some structures moving
towards the end using index variable

so after I read the structure, I increment the index by its size. Then
I want to find out whether there are more structures by seeing if rest
of bits are 0 or not.

what is the standard way to do it?
 
I have byte[1024] and I am parsing it for some structures moving
towards the end using index variable

so after I read the structure, I increment the index by its size. Then
I want to find out whether there are more structures by seeing if rest
of bits are 0 or not.

what is the standard way to do it?

Here is some code, tell me if this answers your question because I
can't really tell what your question is:

using System;

public static class EntryPoint {
static byte[] bytes;

public static void Main() {
bytes = new byte[1024];
FillABlock(ref bytes, 100); // put 100 bytes of junk in bytes[0]--
// bytes[99], leave bytes[100]--bytes[1023] empty

bool r1 = IsTheRestEmpty(bytes, 80); // returns false
bool r2 = IsTheRestEmpty(bytes, 100); // returns true

Console.WriteLine(r1); // False
Console.WriteLine(r2); // True
}

public static void FillABlock(ref byte[] buf, int size) {
if(size > buf.Length)
throw new ArgumentOutOfRangeException("size",
"Cannot be > buf.Length");

for(int i = 0; i < size; i++) {
buf = (byte)(i % 256);
}
}

public static bool IsTheRestEmpty(byte[] buf, int startingFrom) {
for(int i = startingFrom; i < buf.Length; i++) {
if(buf != 0) return(false);
}

return(true);
}
}
 
sasha said:
I have byte[1024] and I am parsing it for some structures moving
towards the end using index variable

so after I read the structure, I increment the index by its size. Then
I want to find out whether there are more structures by seeing if rest
of bits are 0 or not.

what is the standard way to do it?

Change the logic so you have the length of the actual
data.

Assuming trailing zero bytes can not be valid data is a disaster
waiting to happen.

Arne
 
Back
Top