I have this unsafe code that I need to make safe so it can execute. I know about the fixed block, but I don’t know how to implement it:
private static unsafe void PrintHex(byte* data, uint len)
{
uint ctr;
string sep;
if (len > 64)
{
len = 64;
}
for (ctr = 0; ctr < len; ctr++)
{
if (((ctr & 7) == 0) && (ctr != 0))
{
sep = "\n";
}
else
{
sep = "";
}
Console.Error.WriteLine("{0}{1:X}", sep, data[ctr]);
}
Console.Error.WriteLine("\n\n");
}
It’s the responsibility of the calling code to ‘fix’ or ‘pin’ the pointer before calling your method, as in this example from the MSDN page for
fixed: