Dividing array into sub-arrays
-
Hi, I am trying to pass multiple sections of one big byte[] into a function that takes byte[] as a parameter. This is what I am trying to do: method( bigBuffer[ bufferIndex ], bufferSize ) where method is declared as void method( byte[], int ) The code above results in a compilation error because a byte cannot be converted into a byte[]. Is there a way to do this without forcing me to break the bigBuffer down into smaller byte[] byte by byte? Thanks!
-
Hi, I am trying to pass multiple sections of one big byte[] into a function that takes byte[] as a parameter. This is what I am trying to do: method( bigBuffer[ bufferIndex ], bufferSize ) where method is declared as void method( byte[], int ) The code above results in a compilation error because a byte cannot be converted into a byte[]. Is there a way to do this without forcing me to break the bigBuffer down into smaller byte[] byte by byte? Thanks!
SebbaP wrote: I am trying to pass multiple sections of one big byte[] into a function that takes byte[] as a parameter. First change the signature of the method you want to call in something like this...
void Foo(byte[] byteArray, int offset, int length)
...where offset is the starting index and length the number of bytes to process. You can then call it like this:
Foo(bigBuffer, offset, length);
If you can't (or don't want to) change the function that is going to process the sections of your array you might use the static method
Array.Copy
[^]. However (as the name implies) it copies (a part) of the array and therefore is slower and the callee will not be able to manipulate the original data. Best regards Dennis -
SebbaP wrote: I am trying to pass multiple sections of one big byte[] into a function that takes byte[] as a parameter. First change the signature of the method you want to call in something like this...
void Foo(byte[] byteArray, int offset, int length)
...where offset is the starting index and length the number of bytes to process. You can then call it like this:
Foo(bigBuffer, offset, length);
If you can't (or don't want to) change the function that is going to process the sections of your array you might use the static method
Array.Copy
[^]. However (as the name implies) it copies (a part) of the array and therefore is slower and the callee will not be able to manipulate the original data. Best regards DennisThank you Dennis! I cannot (or rather, would not like to) change the method signature, and as you mentioned, copying each element is slow. But that is what I have settled for (I would end up copying each element in the partitioned byte[] anyway inside Foo), until I - or someone else for that matter - comes up with a quicker solution. Anyone? Thanks!
-
Thank you Dennis! I cannot (or rather, would not like to) change the method signature, and as you mentioned, copying each element is slow. But that is what I have settled for (I would end up copying each element in the partitioned byte[] anyway inside Foo), until I - or someone else for that matter - comes up with a quicker solution. Anyone? Thanks!
SebbaP wrote: I cannot (or rather, would not like to) change the method signature, and as you mentioned, copying each element is slow. But that is what I have settled for (I would end up copying each element in the partitioned byte[] anyway inside Foo), until I - or someone else for that matter - comes up with a quicker solution. Copying is slower but that does not necessarily mean that it's too slow for you. Although I'd still suggest that you should consider refactoring the method you want to call, you should give
Array.Copy()
a try. Somehow I doubt that it copies each element. I would rather expect a simple memcopy which should not get you into trouble unless you want to copy a few gigabytes per second. If you take a look at howArray.Copy()
works you'll find out that it is implemented directly in the CLR. The method is overloaded however it always results in a call of the following method:[MethodImpl(MethodImplOptions.InternalCall),
ReliabilityContract(Consistency.MayCorruptInstance, CER.MayFail)]
internal static extern void Copy(Array sourceArray, int sourceIndex,
Array destinationArray, int destinationIndex,
int length, bool reliable);Best regards Dennis
-
Hi, I am trying to pass multiple sections of one big byte[] into a function that takes byte[] as a parameter. This is what I am trying to do: method( bigBuffer[ bufferIndex ], bufferSize ) where method is declared as void method( byte[], int ) The code above results in a compilation error because a byte cannot be converted into a byte[]. Is there a way to do this without forcing me to break the bigBuffer down into smaller byte[] byte by byte? Thanks!
2 more ways: 1. use the second variable as an index instead, and define a constant buffersize. 2. use a static field to store offset info. top secret
Download xacc-ide 0.0.3 now!
See some screenshots