Converting Pointers in C to C# -


i need convert c uses pointers c#.

essentiay given buffer of unformated data , need treat if if formatted. read froma file may not come emmbedded c application running different hardware.

in example need treat array of long.

i c take create pointe long , cast address of buffer.

but in c# not alowed take address of managed object , wont let me cast byte[] long[]

so how in c#

void testfunction(void) {     char buffer[256];     printbufferaslongpointer(buffer);     printbufferaslongarray(buffer); }  void printbufferaslongpointer(char *buffer) {     long *ptr = (long*)buffer;     for(int count = 0; count < 64; count++)     {         printf(" %x", *ptr++);     } }  void printbufferaslongarray(char *buffer) {     for(int index = 0; index < 64; index++)     {         printf(" %x", buffer[index]);     } } 

you can still use pointers in c# if you're careful (and enable 'unsafe code' in project options):

private static unsafe void printbufferaslongarray(byte[] buffer) {     fixed (byte* pbuffer = buffer)     {         long* longdata = (long*)pbuffer;         (int index = 0; index < 64; index++)             console.writeline(" {0:x}", longdata[index]);     } } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

ios - Possible to get UIButton sizeThatFits to work? -