이미지를 전송하는 장치로 작업하고 있으며 이미지를 요청할 때 이미지 데이터 이전에 문서화되지 않은 정보가 있습니다. 필자는 바이너리 데이터를보고 내부의 이미지 헤더 정보를 확인함으로써이 사실을 깨달을 수있었습니다.바이트 배열의 확장 메소드에 문제가 있음
나는 원래 정상적인 방법을 사용했고 확장 방법으로 변환했다. 여기서 원래의 질문은 배열을 첫 번째 매개 변수로 가지지 않는다고 불평하는 컴파일러와 관련이 있습니다 (Byte []를 가졌음). 그러나 오류가 발생하여 호출 코드에서 첫 번째 인수를 삭제하는 것을 잊어 버렸습니다. 즉, 내가 가진 데 사용 :
어쨌든buffer.RemoveUpToByteArray(buffer, new byte[] { 0x42, 0x4D });
, 즉 내가 있었다 나는 내 실수를 깨달았 때문에 모두가 지금은 고정입니다 :
Byte[] new_buffer = RemoveUpToByteArray(buffer, new byte[] { 0x42, 0x4D });
및 확장 방법을 변경 한 후, 내가 잘못 사용했다
SO에 코드 예제를 입력하십시오. 그러나 확장 메서드 및 참조와 값 형식에 대한 이해가 부족하다는 새로운 문제가 있습니다. 코드는 다음과 같습니다.public static void RemoveFromByteArrayUntil(this Byte[] array, Byte[] until)
{
Debug.Assert(until.Count() > 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<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();
}
내 문제는 이제 확장 메서드에 대한이 인수가 값으로 전달된다는 것입니다. 어떤 배열 포인트를 재 할당하고 싶지만,이 경우 배열의 데이터를 대신 조작해야하는 것처럼 보입니다.
답이 아니라 단지 관찰 : RemoveUpToByteArray가 호출 될 때마다 새 바이트 배열을 만들어야하는 것으로 보입니다. 배열의 인덱스로 정수를 사용하면 읽은 거리를 추적하는 것이 더 효율적입니다. –
@Dave : 컴파일러 오류가 발생하는 곳이 너무 구체적이지 않습니다. 나는 그것이 * 호출 사이트 *에서이고 확장 메서드가 아닌 것으로 추측하고 있습니다. 그것이 맞다하더라도, 당신은 어느 쪽의 코드도 보이지 않는다. 바이트 배열에 대한 확장 메서드를 만들고 쉽게 실행할 수 있으므로 대답을 얻기 위해이 부분을 처리해야합니다. – casperOne
확인, 문제 없습니다! * 실제 * 코드를 게시하겠습니다. :) 의견을 보내 주셔서 감사합니다. – Dave