0

1つのAPDUを送信したいのですが、応答が返されます。比較をログに記録するAPIで最後の2バイトをチェックしたいと思います。

byte[] response = Transmit(apdu);

//response here comes will be 0x00 0x01 0x02 0x03 0x04 
//response.Length will be 5


byte[] expectedResponse = { 0x03, 0x04 };

int index = (response.Length)-2;

Log.CheckLastTwoBytes(response[index],expectedResponse);

//The declaration of CheckLastTwoBytes is 
//public static void CheckLastTwoBytes(byte[] Response, byte[] ExpResp)

これは無効な引数のエラーです。最後の2バイトをAPIに渡すにはどうすればよいですか?

4

5 に答える 5

3

Array.Copyを使用する

byte[] newArray = new byte[2];
Array.Copy(response, response.Length-2, newArray, 2);
Log.CheckLastTwoBytes(newArray,expectedResponse);
于 2012-11-29T03:18:39.287 に答える
1

のタイプresponse[index]byte(ではない byte[])なので、そのエラーが発生するのは当然です。

パラメータLog.CheckLastTwoBytesの最後の2バイトだけを実際にチェックする場合は、次のように渡す必要があります。Responseresponse

   Log.CheckLastTwoBytes(response, expectedResponse)
于 2012-11-29T03:23:41.040 に答える
1
new ArraySegment<byte>(response, response.Length - 2, 2).Array

編集:これを気にしないでください、明らか.Arrayに元の配列全体を返し、スライスは返しません。byte []の代わりにArraySegmentを受け入れるように、他のメソッドを変更する必要があります。

于 2012-11-29T03:26:32.220 に答える
1

あなたはそのようなサブアレイを持つことはできません、いや...

最初の解決策、明らかなもの:

var tmp = new byte[] { response[response.Length - 2],
                       response[response.Length - 1] };

Log.CheckLastTwoBytes(tmp, expectedResponse);

または、これを行うことができます:

response[0] = response[response.Length - 2];
response[1] = response[response.Length - 1];

Log.CheckLastTwoBytes(response, expectedResponse);

この関数は正確な長さなどをチェックしない可能性があるため、データを破棄する必要がない場合は、最後の2バイトを最初の2バイトとして配置できます。

于 2012-11-29T03:44:51.727 に答える
0

または、代わりに、linqを使用できます。

byte[] lastTwoBytes = response.Skip(response.Length-2).Take(2).ToArray();
于 2012-11-29T03:22:29.500 に答える