How To Serialize DotNet Types into Byte Arrays?

  • Thread starter Thread starter Leon_Amirreza
  • Start date Start date
Leon,

Serialization has a very specific meaning in .NET, and is different from
what you are trying to do (which was answered in your next post). The
process of serialization in .NET will write a value, but also write type
information as well into the serialization stream, such that predicting the
size of the outcome is tedious, at best. Also, it is not guaranteed that
you will have the same layout in the serialized form as you do in memory.

If you want to truly serialize the contents of the uint into a byte
array, then you would use a BinaryFormatter instance to serialize to a
MemoryStream.

However, what you want to do is use the BitConverter class, as pointed
out in the answers to your next post.
 
Hi,

Uint value into a Byte[4]:

byte[] bytes = System.BitConverter.GetBytes(<int_or_such_value>);

Other approach is to use BinaryFormatter.Serialize() (but it will be more
then 4 bytes)

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



L> How can I serialize a uint Type into a Byte[4]?
L>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top