0

ID を文字列として送信する必要がある tcp it サービスを使用しようとしています。サンプルコードから以下のメソッドを取得しました。問題は、「4000」、「2000」、「3000」などの 4 文字の数字を含む文字列を入力するとメソッドは機能しますが、4 文字未満の文字列を入力すると「1」、「20」、または「300」になることです。戻り値

System.ArgumentException: 宛先配列の長さが十分ではありません。destIndex と長さ、および配列の下限を確認してください。

 public byte[] prepNetworkStreamBuffer(string reqiiredID) {
        byte[] id = UTF8Encoding.UTF8.GetBytes(reqiiredID);
        int l = id.Length;
        byte[] idb = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder(l));

        byte[] buff = new byte[1 + 1 + id.Length + l];
        buff[0] = 0;
        buff[1] = (byte)VerificationServiceCommands.addIDtoAFIS;
        idb.CopyTo(buff, 1 + 1);
        id.CopyTo(buff, 1 + 1 + idb.Length);

        return buff;
    }
4

3 に答える 3

0

idbと を にコピーしidていますがbuff、サイズは のみです2*id.Length + 2

したがって、idがサイズ 3 しかない場合、サイズ 4 のbuffに合わせるには小さすぎますidb

あなたがしたい:

byte[] buff = new byte[1 + 1 + id.Length + idb.Length];
于 2012-08-04T14:43:06.957 に答える
0
    public static bool TryGetArray(ref SomeObject[] source )
    {
        try
        {
            var localSource = new List<SomeObject>{new SomeObject(), new SomeObject()};

            var temp = new SomeObject[localSource.Count + source.Length];
            Array.Copy(source, temp, source.Length);
            Array.ConstrainedCopy(localSource.ToArray(), 0, temp, source.Length, localSource.Count);
            source = temp;
        }
        catch
        {
            return false;
        }
        return true;
    }
于 2014-04-10T16:43:55.217 に答える