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
Post a Comment