Michal Jaeschke [mailto:[EMAIL PROTECTED]] wrote:

> How convert in "unsafe context" byte[] to sbyte*;
> I need to have pointer to array of sbyte.
> Any ideas?

Sure, here's the basics:

<codeSnippet language="C#">
private unsafe void Test()
{
  byte[] bytes = new byte[] { 1, 2, 3 };

  fixed(byte* pbytes = &bytes[0])
  {
    sbyte* psbytes = (sbyte*)pbytes;

    Debug.WriteLine(psbytes[1]); // writes 2
  }
}
</codeSnippet>

First you need to mark your method as unsafe (or you can mark a block of
code as unsafe), next you use the fixed block to pin the managed byte array
so that the GC won't move it around in memory within the scope of the block,
then you use the basic C++ address of operand (&) to get a pointer to first
element of the byte array, finally you can cast to an sbyte* using
straightforward casting.

HTH,
Drew

[ .NET MVP | weblog: http://radio.weblogs.com/0104813/ ]

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to