convert byte[] to short[]
-
Hi all, I need to perfome simple-looking task in C#: Converting byte array to short array - meaning that if my byte array has 100 cells, my short array will have 50 cells. If it was C/C++ it was simple, using pointers. However in C# I just can't do it - I've already tried using 'unsafe' block and memory pinning using GCHandle, with no success. Another thing that bothers me in the solutions I found is that the conversion is done using memory copying, which is not necessary, since all the information is already there! I'll be grateful for any solution! Thanks, Eyal.
-
Hi all, I need to perfome simple-looking task in C#: Converting byte array to short array - meaning that if my byte array has 100 cells, my short array will have 50 cells. If it was C/C++ it was simple, using pointers. However in C# I just can't do it - I've already tried using 'unsafe' block and memory pinning using GCHandle, with no success. Another thing that bothers me in the solutions I found is that the conversion is done using memory copying, which is not necessary, since all the information is already there! I'll be grateful for any solution! Thanks, Eyal.
-
this might work
private void foo()
{
byte[] b = new byte[100];
IntPtr hnd = Marshal.AllocHGlobal(Marshal.SizeOf(b));
Marshal.Copy(b,0,hnd,b.Length);
short[] s = new short[50];
Marshal.Copy(hnd,s,0,s.Length);
Marshal.FreeHGlobal(hnd);
} -
Hi, When using the code you suggested I get the following error: Type 'System.Byte[]' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed. Eyal.
-
Hi, just wanted to say thanks - this works! the only problem I still have with this solution is that what it does is copying the entire byte[] memory to another location in the memory - what appears to be redundant, since it's already there! in C/C++ all I had to do is one pointer manipulation - can't I do it here as well? is it because the existance of garbage collector? Thanks! Eyal.
-
Hi, just wanted to say thanks - this works! the only problem I still have with this solution is that what it does is copying the entire byte[] memory to another location in the memory - what appears to be redundant, since it's already there! in C/C++ all I had to do is one pointer manipulation - can't I do it here as well? is it because the existance of garbage collector? Thanks! Eyal.
Hi I think it has something to do with the Memory Management in .Net (which includes the garbage collector). As far as i understand it, object are not located fix in Memory. this means an Array of byte can "move" inside the memory. That's why you get an exception when you try to get the Memory Address of an Item of the Array. but you can force .Net to keep an Array at the same Location for a limited time. for this there's the
fixed
br mode="hold" />i'm not quite sure about this, but i think fixing is only for reading addresses.. so i don't think it's possible to set the first entry of a short array to the address of the first item of a Byte Array. but maybe google knows more if you ask it about fixed unsafe array manipulation or something :) greets m@u