'C# startIndex + length > this.length
I`m experimenting a weird issue. I code a huge amount of data ( an struct ) into an hex string and upload to the server as varbin. Then I download it and, sometimes it cant be decoded. So I decided to code some prints:
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);
}
}
then in the try/catch of the server response coroutine I get this:
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
I don't know why it can't read after half of the string, only thought about the size, because with not so big sets of data it works like a charm.
log2(64000) is approximately 16. I'm assuming max length for a C# string is 16 bits but surprise! I found it is 32 bits. Thinking about string.substring and string.length as not at all correlated methods, but why?
Solution 1:[1]
start: 63000 end: 64000
message.Substring(start, end)
The second argument to Substring is the length of the string to extract. You are telling it to start at position 63000 and retrieve the next 64000 characters.
Solution 2:[2]
Substring(Int32, Int32) retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
public string Substring (int startIndex, int length);
It returns a string that is equivalent to the substring of length length that begins at startIndex in this instance. Also, throws ArgumentOutOfRangeException if startIndex + length indicates a position that not within the string.
So, If you are getting System.ArgumentOutOfRangeException: startIndex + length > this.length then you have to check the length of the substring with startIndex + length value.
For example:
String value = "This is a string.";
int startIndex = 5;
int length = 2;
String substring = value.Substring(startIndex, length);
Console.WriteLine(substring);
// Output: is
length = 15;
substring = value.Substring(startIndex, length);
Console.WriteLine(substring);
// Output: System.ArgumentOutOfRangeException: startIndex + length > this.length
// Now, startIndex + length > this.length => 5 + 15 > 17
// Here you are telling that start at position 5 and retrieve the next 15 characters.
// But we have only 12 characters from the position 5.
For more: https://docs.microsoft.com/en-us/dotnet/api/system.string.substring?view=net-6.0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dazedandconfused |
| Solution 2 | Codemaker |
