473,394 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

byte array to struct

16
this is in reference to one of the posts i found in the .NET forum
i need to send structure across thru the socket....


static byte [] StructureToByteArray(object obj)
>
> {
>
> int len = Marshal.SizeOf(obj);
>
> byte [] arr = new byte[len];
>
> IntPtr ptr = Marshal.AllocHGlobal(len);
>
> Marshal.StructureToPtr(obj, ptr, true);
>
> Marshal.Copy(ptr, arr, 0, len);
>
> Marshal.FreeHGlobal(ptr);
>
> return arr;
>
> }
>
> static void ByteArrayToStructure(byte [] bytearray, ref object obj)
>
> {
>
> int len = Marshal.SizeOf(obj);
>
> IntPtr i = Marshal.AllocHGlobal(len);
>
> Marshal.Copy(bytearray,0, i,len);
>
> obj = Marshal.PtrToStructure(i, obj.GetType());
>
> Marshal.FreeHGlobal(i);
>
> }
>
>i was able to convert the struct to byte array but to perform the vice versa wat argument shud be sent in ref object??
is there any other method or modification i could do?
thanks in advance
Mar 5 '08 #1
1 3043
That object is an object of type you want to cast your byte array to. There is another way so you don't need that object.

Expand|Select|Wrap|Line Numbers
  1. static object ByteArrayToStructure(byte[] bytearray)
  2. {
  3.   IntPtr ptr = IntPtr.Zero;
  4.  
  5.   try
  6.   {
  7.     ptr = Marshal.AllocHGlobal(bytearray.Length);
  8.     Marshal.Copy(bytearray, 0, ptr, bytearray.Length);
  9.  
  10.     return Marshal.PtrToStructure(ptr, typeof(SomeType));
  11.   }
  12.   finally
  13.   {
  14.     if(ptr != IntPtr.Zero)
  15.     {
  16.       Marshal.FreeHGlobal(ptr);
  17.     }
  18.   }
  19. }
  20.  
Where the SomeType in typeof(SomeType) is the type of object you want to convert your byte array to.


this is in reference to one of the posts i found in the .NET forum
i need to send structure across thru the socket....


static byte [] StructureToByteArray(object obj)
>
> {
>
> int len = Marshal.SizeOf(obj);
>
> byte [] arr = new byte[len];
>
> IntPtr ptr = Marshal.AllocHGlobal(len);
>
> Marshal.StructureToPtr(obj, ptr, true);
>
> Marshal.Copy(ptr, arr, 0, len);
>
> Marshal.FreeHGlobal(ptr);
>
> return arr;
>
> }
>
> static void ByteArrayToStructure(byte [] bytearray, ref object obj)
>
> {
>
> int len = Marshal.SizeOf(obj);
>
> IntPtr i = Marshal.AllocHGlobal(len);
>
> Marshal.Copy(bytearray,0, i,len);
>
> obj = Marshal.PtrToStructure(i, obj.GetType());
>
> Marshal.FreeHGlobal(i);
>
> }
>
>i was able to convert the struct to byte array but to perform the vice versa wat argument shud be sent in ref object??
is there any other method or modification i could do?
thanks in advance
Mar 5 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Ray Z | last post by:
So far, I get the idea that if I want to use both the unmanaged and managed memory, I can not avoid memory copy. But I DO need to avoid it. I get a idea that maybe I could use "union" to convert...
3
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
7
by: Prabhu | last post by:
Hi, I have to send a structure through TCPClient socket. we can send only byte array through the socket, So please any one can help me by telling How to convert a struct object into an byte...
8
by: Ben Terry | last post by:
What's the most efficient way to transfer data from a byte to a struct? The struct is rather complex--contains other structs as well as byte members. I've tried to use Marshal.Copy and an IntPtr...
5
by: Amil Hanish | last post by:
I have a huge byte array from another API that contains structs of differing sizes. I'd like to create n structs based on data in the byte array. But how? For example, if I want to take bytes...
1
by: Zoser | last post by:
When marshalling structure from C to .NET, I get the following error: System.ArgumentException: Type could not be marshaled because the length of an embedded array instance does not match the...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
3
by: mjaaland | last post by:
Hi, I hope someone can help me out with this one! Step 1: I get a struct from unmanaged code looking like this. public unsafe struct KEY { public UInt16 idkey_len;...
9
by: ssubbarayan | last post by:
Hi all, I am trying a program to convert floating point values to a byte array and printing the same to the screen.The idea behind this is we already have an existing function which can do byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.