C#startIndex + length> this.length

时间:2016-11-03 14:33:19

标签: c#

我正在尝试一个奇怪的问题。我将大量数据(结构)编码为十六进制字符串,并作为varbin上传到服务器。然后我下载它,有时它无法解码。所以我决定编写一些打印件:

public static void d(String TAG, String message)
{
    int maxLogSize = 1000;
    for (int i = 0; i <= message.Length / maxLogSize; i++)
    {
        int start = i * maxLogSize;
        int end = (i + 1) * maxLogSize;
        if (end > message.Length - 1)
            end = message.Length - 1;
        //end = end > message.Count() ? message.Count() : end;
        print(message.Substring(start, end));
        print("start: " + start.ToString() + " end: " + end.ToString() + " size: " + message.Length);
    }
}

然后在服务器响应协同程序的try / catch中我得到了这个:

11-03 15:03:02.616 13103-13117/com.Mindfunk.minus I/Unity: start: 63000 end: 64000 size: 127834

(Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 37)
11-03 15:03:02.636 13103-13117/com.Mindfunk.minus I/Unity: There was an error decoding this challenge replay . Maybe it is corrupted.
System.ArgumentOutOfRangeException: startIndex + length > this.length
Parameter name: length
    at System.String.Substring (Int32 startIndex, Int32 length) [0x00000] in <filename unknown>:0 
    at DBConnection+Log.d (System.String TAG, System.String message) [0x00000] in <filename unknown>:0 
    at DBConnection+<DownloadChallengeReplay>c__IteratorE.MoveNext () [0x00000] in <filename unknown>:0 

我不知道为什么它只能在字符串的一半之后阅读,只考虑尺寸,因为没有那么大的数据集就像魅力一样。

log2(64000)约为16.我假设C#字符串的最大长度为16位,但令人惊讶!我发现它是32位。考虑string.substringstring.length并非所有相关方法,但为什么?

1 个答案:

答案 0 :(得分:7)

  

开始:63000结束:64000

 message.Substring(start, end)

Substring的第二个参数是要提取的字符串的 length 。您告诉它从位置63000开始并检索接下来的64000个字符。