Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving trouble with extension methods for byte arrays
    primarykey
    data
    text
    <p>I'm working with a device that sends back an image, and when I request an image, there is some undocumented information that comes before the image data. I was only able to realize this by looking through the binary data and identifying the image header information inside.</p> <p>I originally had a normal method and converted it to an extension method. The original question here was related to the compiler complaining about not having Array as the first parameter (I had Byte[]), but it turns out that I had made an error and forgot to delete the first argument in the calling code. In other words, I used to have:</p> <pre><code>Byte[] new_buffer = RemoveUpToByteArray(buffer, new byte[] { 0x42, 0x4D }); </code></pre> <p>and after changing to an extension method, I had erroneously used:</p> <pre><code>buffer.RemoveUpToByteArray( buffer, new byte[] { 0x42, 0x4D }); </code></pre> <p>Anyhow, that's all fixed now because I realized my mistake as I was entering the code example into SO. <strong>However</strong>, I have a new problem that is simply lack of understanding of extension methods and reference vs. value types. Here's the code:</p> <pre><code>public static void RemoveFromByteArrayUntil(this Byte[] array, Byte[] until) { Debug.Assert(until.Count() &gt; 0); int num_header_bytes = until.Count(); int header_start_pos = 0; // the position of the header bytes, defined by [until] byte first_header_byte = until[0]; while(header_start_pos != -1) { header_start_pos = Array.IndexOf(array, first_header_byte, header_start_pos); if(header_start_pos == -1) break; // if we get here, then we've found the first header byte, and we need to look // for the next ones sequentially for(int header_ctr=1; header_ctr&lt;num_header_bytes; header_ctr++) { // we're going to loop over each of the header bytes, but will // bail out of this loop if there isn't a match if(array[header_start_pos + header_ctr] != until[header_ctr]) { // no match, so bail out. but before doing that, advance // header_start_pos so the outer loop won't find the same // occurrence of the first header byte over and over again header_start_pos++; break; } } // if we get here, we've found the header! // create a new byte array of the new size int new_size = array.Count() - header_start_pos; byte[] output_array = new byte[new_size]; Array.Copy(array, header_start_pos, output_array, 0, new_size); // here is my problem -- I want to change what array points to, but // when this code returns, array goes back to its original value, which // leads me to believe that the first argument is passed by value. array = output_array; return; } // if we get here, we didn't find a header, so throw an exception throw new HeaderNotInByteArrayException(); } </code></pre> <p>My problem now is that it looks like the first this argument to the extension method is passed by value. I want to reassign what array points to, but in this case, it looks like I'll have to just manipulate array's data instead.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload