Casting char/byte array to IntPtr and convert char array to byte array
-
Monday, January 21, 2008 6:46 PMHi,1 - What is the fast way to copy/cast array of chars to array of bytes (not using loop) ?
byte
[] ByteArray <<<==== char[] ByteArray12- is it posible to cast array of chars / array of bytes to IntPtr ?
IntPtr pointer = (?????) ByteArray ;
Best Regards,
Joe
All Replies
-
Monday, January 21, 2008 7:19 PMModerator
You can't cast a byte array to a char array and vice versa because a char is 16-bits and a byte is 8-bits.
-
Monday, January 21, 2008 7:23 PMAs I know char is 8 bit , 0x00 to 0xFF !!!!!
-
Monday, January 21, 2008 7:26 PMModeratorchar in C# is 16-bits, not 8-bits. Char is Unicode in .NET.
-
Monday, January 21, 2008 7:31 PMI can't imaging that there are no function that I can use to use the pointer ? the data is residint in the memmory as ablock of bytes, I should have the ability to use those data in any way I want I can use them as char or as just number . so It should be a way to get the pointer from char array make it byte array :char [] aray = {.......................}byte [] arrayBytearrayByte = &array or (byte*)(void*)array :-(from this poit I can use arrayByte.Length -> this can be aray.Length*2
-
Monday, January 21, 2008 7:35 PMModerator
You could use Marshal.UnsafeAddrOfPinnedArrayElement(array, 0) to get a memory pointer to the array. You can do what ever you want from that pointer if you want. -
Monday, January 21, 2008 7:51 PMThanks a lot, you help me .//Unmanage code send me an array of unsigned char represent RowDatavoid DrawImage(char [] arrayOfCharsFromUnmanageCode)
{
byte[] bmpByteArray = new byte[width*height];
IntPtr pSource =System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(arrayOfCharsFromUnmanageCode, 0);
System.Runtime.InteropServices.Marshal.Copy(pSource,bmpByteArray,0,Math.Min(arrayOfCharsFromUnmanageCode.Length,bmpByteArray.Length));
......} -
Thursday, June 26, 2008 6:17 AMI still have my original Kerningham and Ritchie Programming in C book. Life was much simpler then...
I loved programming in C because it gave me such flexibility and I was able to get close to the memory...
Now I have to bend over backwards in order to set bytes in a message and to cast them to ints, etc.
Is there anything written about bit/byte manipulation in C#?
Thanks!
Mechi
Mechi -
Tuesday, September 09, 2008 7:18 AMJoe,
What you want is called marshalling.
Please read the following article:
http://blog.rednael.com/2008/08/29/MarshallingUsingNativeDLLsInNET.aspx
It's an in depth article about marshalling between native code and managed code. It shows which types are interoperable, how to import a DLL, how to pass strings, how to pass structures and how to de-reference pointers.
Basically all the information you need.

