Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 979521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:13:54+00:00 2026-05-16T04:13:54+00:00

I am trying to convert class to byte but not worked. I receive a

  • 0

I am trying to convert class to byte but not worked.

I receive a byte[] (b) with zero length . why ?

void start()
{

    Item item = new Item();
    item.files.Add(@"test");
    byte[] b = ObjectToByteArray(item);  // b will have zero length 

}


    [Serializable]
    public class Item : ISerializable
    {
        public Item()
        {
            files = new List<string>();
            Exclude = false;
            CleanEmptyFolder = false;
        }
        public List<string> files;
        public string MusicProfileName;
        public bool Exclude;

        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("files", files);
            info.AddValue("MusicProfileName", MusicProfileName);
            info.AddValue("Exclude", Exclude);
        }

        #endregion
    }

    public byte[] ObjectToByteArray(object _Object)
    {
        using (var stream = new MemoryStream())
        {
            // serialize object 
            var formatter = new BinaryFormatter();
            formatter.Serialize(stream, _Object);

            // get a byte array
            var bytes = new byte[stream.Length];
            using (BinaryReader br = new BinaryReader(stream))
            {
                bytes = br.ReadBytes(Convert.ToInt32(stream.Length));
            }

            return bytes;
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:13:55+00:00Added an answer on May 16, 2026 at 4:13 am

    Try using MemoryStream.ToArray instead of trying to read from the MemoryStream with a BinaryReader:

    return stream.ToArray();
    

    The problem is that you aren’t resetting the stream, so it’s trying to read from the end instead of the beginning. You could also seek back to the beginning of the stream:

    stream.Seek(0, SeekOrigin.Begin);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert some code that worked great in VB, but I can't
I'm trying to convert an Image object to a byte array then back to
i'm trying to convert a 'TableController'-Class we used (without ORM) to generate dynamic SQL
I am trying convert the following Collatz Conjecture algorithm from: public class CollatzConjexture {
I am trying convert the following Collatz Conjecture algorithm from: public class CollatzConjexture {
I'm trying to convert the following C# into F#: public class Matrix { double[,]
I am new to jQuery. I am trying to convert this jQuery code so
Is it possible to extend more then one abstract class? I'm trying to convert
I've been trying to convert SVG images to PNG using C#, without having to
I'm trying to convert old QuickTime framework code to the 64-bit Cocoa-based QTKit on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.