C
C#2y ago
Matt

❔ Array.IndexOf being weird

I'm very confused. Been trying to use Array.IndexOf to find a null terminator in a byte array
int endOfString = Array.IndexOf(Data, 0x0, EntryNameTableOffset + entry.NameTableOffset);
int endOfString = Array.IndexOf(Data, 0x0, EntryNameTableOffset + entry.NameTableOffset);
But it fails to find the byte and returns -1 Doing this however, it finds the byte... O_O Why does Array.IndexOf not work in this situation?
for (int x = EntryNameTableOffset + entry.NameTableOffset; x < Data.Length; x++)
{
if (Data[x] == 0x0)
{
endOfString = x;
break;
}
}
for (int x = EntryNameTableOffset + entry.NameTableOffset; x < Data.Length; x++)
{
if (Data[x] == 0x0)
{
endOfString = x;
break;
}
}
11 Replies
occluder
occluder2y ago
.IndexOf() has a generic overload. Do .IndexOf<byte>() and it should work
Matt
MattOP2y ago
you're a lifesaver... weird that it doesn't detect the type. What is the type by default?
occluder
occluder2y ago
It looks like it doesn't care about that from the parameters, it's looking for an 'object?' in 'Array'
phaseshift
phaseshift2y ago
Depends on your variable type whether the generic overload would be picked
Matt
MattOP2y ago
That seems dumb to me. So in some cases we just have to explicitly state the type but in other cases it will figure it out? C# be quirky
occluder
occluder2y ago
Also relevant. When you put in 0x0 it defaulted to an int
Matt
MattOP2y ago
that makes more sense
occluder
occluder2y ago
So it couldn't apply IndexOf<byte> to byte[] and int
Matt
MattOP2y ago
gotcha. Thank you for the help 🙂
phaseshift
phaseshift2y ago
What type is Data declared as?
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server