Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Mike

[C#] How do I convert to bytes?

This topic is 5234 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In C/C++ if i want to send a struct over a TCP/IP connection i can just do (in psuedo code) send( (char*)&struct_instance, sizeof( struct_type ) ); Then on the other end i can just do recv( (char*)&struct_instance, bla, bla ); this makes it nice and easy to send data over the internet (like, a packet header for instance). Now how in the heck do I do this in C#? I put this here instead of the network forum because the general question is simply, how do I get the chunk of memory holding a piece of data? thanks, mike.

Share this post


Link to post
Share on other sites
Advertisement
With some tricks you can easily fill C# structures from byte arrays:

Add a System.Runtime.InteropServices.StructLayoutAttribute to your class/struct definitions:


[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class Foo
{

//add Attributes to specify length of strings and arrays.

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string s;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public int[] i;
}



To read raw binary data, I just extend a BinaryReader:

   
public class RawReader : BinaryReader
{
public RawReader(Stream input): base(input)
{
}

public RawReader(Stream input, System.Text.Encoding enc): base(input, enc)
{
}

public string ReadRawString(int bytes)
{
return Encoding.ASCII.GetString(ReadBytes(bytes)).TrimEnd('\0');
}

public object ReadRaw(Type type)
{
int rawSize = Marshal.SizeOf(type);
byte[] rawData = ReadBytes(rawSize);

if(rawSize > rawData.Length)
return null;

IntPtr buffer = Marshal.AllocHGlobal(rawSize);
Marshal.Copy(rawData, 0, buffer, rawSize);
object obj = Marshal.PtrToStructure(buffer, type);
Marshal.FreeHGlobal(buffer);
return obj;
}
}

Writing raw data:

   
public class RawWriter : BinaryWriter
{
public RawWriter(Stream output): base(output)
{
}

public RawWriter(Stream output, System.Text.Encoding enc): base(output, enc)
{
}

public void WriteRawString(string str)
{
Write(Encoding.ASCII.GetBytes(str));
}

public void WriteRaw(Object obj)
{
int rawSize = Marshal.SizeOf(obj);
IntPtr buffer = Marshal.AllocHGlobal(rawSize);
Marshal.StructureToPtr(obj, buffer, false);
byte[] rawData = new byte[rawSize];
Marshal.Copy(buffer, rawData, 0, rawSize);
Marshal.FreeHGlobal(buffer);
Write(rawData);
}
}


I found this code at: http://web.zdnet.com.au/builder/architect/webservices/story/0,2000035074,20277904,00.htm

[edited by - Serge Asperge on March 22, 2004 11:50:57 AM]

[edited by - Serge Asperge on March 22, 2004 11:51:50 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!