473,503 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

serialization of primitives to a byte array

I am trying to figure out how to serialize data over a socket in C#. Every
example I have seen shows sending a string by converting it to a byte array
and then passing to Send(). Simple enough. But how do I convert longs/shorts
etc. to a byte array? How about structs?

I figure I need to somehow serialize it to a stream or something but I'm a
little lost. When I did this stuff in C++ I would just cast the struct to a
void * and pass it.

I would like to have the knowledge to do it by serialization and also via
casting using the unsafe keyword when performance is a concern. Any examples
out there?

here is a test struct I would like to serialize:

[StructLayout(LayoutKind.Sequential, Pack = 1)]

public struct S

{

public ushort m_us;

public ulong m_ul;

public byte m_b;

public ulong m_ul2;

public ushort m_us2;

}

Thanks.


Nov 15 '05 #1
1 3054
Andy,

The BitConverter class can do this. You could also a BinaryWriter to
write them to a network stream.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2581
by: Fred | last post by:
Are primitives objects? int i = 3; System.out.println(i.getClass()); doesn't compile. Get an error message "int can't be dereferenced" But yet the docs for class Object say: Class Object...
1
1070
by: AndyM | last post by:
I am trying to figure out how to serialize data over a socket in C#. Every example I have seen shows sending a string by converting it to a byte array and then passing to Send(). Simple enough. But...
3
9784
by: Amadelle | last post by:
Hi all and thanks in advance for your help, I am having problems deserializing an object which seems to be serializing just fine. I save the byte array of the serialized object in the database...
2
3461
by: Brian Keating | last post by:
Hello there, I wonder can anyone help point me in correct direction. Basically I have a Managed structure and I want to Serialize to to a byte array, Then I want to read this byte array in my...
11
11925
by: ajou_king | last post by:
I was running some tests on my Win32 1GHZ processor to see how long it would take to transmit objects numerous times via TCP/IP using C# ..NET Remoting vs the C++ trustworthy method of binary...
1
1602
by: Gilad Kapelushnik | last post by:
Hello I need to send data (a whole class) from a Sever to a Client. I convert this class to a binary array using Binary Serialization: public byte ToBytes(){ try{ IFormatter formatter = new...
1
4182
by: elziko | last post by:
My intention is to store an array of singles inside a DataTable so that it can me peristed somehow, maybe XML file, maybe Access/SQL Server I don't know yet so I'm just saving it as an XML file for...
1
7203
by: Dejan | last post by:
Hello! I need to make an ASP.NET XML Web Service serving as an assembly container. Web Service loads the assembly from a given file, and returns that assembly to the caller. Since ASP.NET...
4
1442
by: Mark | last post by:
I'd like to take an instance of a class and store it in a database. I've marked my class as and am using the binary formatting code similar to...
10
3368
by: =?Utf-8?B?QnJpYW4=?= | last post by:
What is the easiest way to convert primitives to a byte array? I tried the BinaryFormatter serialization but it serializes objects so when I serialized an and int it took 54 bytes instead of 4. I...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7089
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
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.