IntPtr to Int - C#

5k Views Asked by At

I have this code :

public uint StringsSize { get; set; }
byte[] buffer = new byte[(IntPtr) XSC.header.StringsSize];

returning the following error :

"Cannot explicitly convert type 'intptr' to 'int'. An explicit conversion exists (are you missing a cast?)"

Any real guidance on fixing this would be appreciated, it's troubled me on several occasions with this tool .. Also, I apologize if this is a duplicated question, I was unable to find a simple fix/solution after several Google searches.

2

There are 2 best solutions below

6
On

Convert the IntPtr to the int type like so:

var buffer = new byte[((IntPtr) XSC.header.StringsSize).ToInt64()];
0
On

You can try like this:

int numInt = intPtrVar.ToInt32();