C
C#15mo 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
occluder15mo ago
.IndexOf() has a generic overload. Do .IndexOf<byte>() and it should work
Matt
Matt15mo ago
you're a lifesaver... weird that it doesn't detect the type. What is the type by default?
occluder
occluder15mo ago
It looks like it doesn't care about that from the parameters, it's looking for an 'object?' in 'Array'
phaseshift
phaseshift15mo ago
Depends on your variable type whether the generic overload would be picked
Matt
Matt15mo 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
occluder15mo ago
Also relevant. When you put in 0x0 it defaulted to an int
Matt
Matt15mo ago
that makes more sense
occluder
occluder15mo ago
So it couldn't apply IndexOf<byte> to byte[] and int
Matt
Matt15mo ago
gotcha. Thank you for the help 🙂
phaseshift
phaseshift15mo ago
What type is Data declared as?
Accord
Accord15mo 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.