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

  • Home
  • SEARCH
  • 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 7862725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:15:08+00:00 2026-06-02T23:15:08+00:00

First time playing with serialization in C#… any help would be greatly appreciated! The

  • 0

First time playing with serialization in C#… any help would be greatly appreciated!
The following are my generic serializer and deserializer:

    public static string SerializeObject<T>(T objectToSerialize)
    {
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream memStr = new MemoryStream();

        try
        {
            bf.Serialize(memStr, objectToSerialize);
            memStr.Position = 0;

            return Convert.ToBase64String(memStr.ToArray());
        }
        finally
        {
            memStr.Close();
        }
    }

    public static T DeserializeObject<T>(string str)
    {
        BinaryFormatter bf = new BinaryFormatter();
        byte[] b = System.Text.Encoding.UTF8.GetBytes(str);
        MemoryStream ms = new MemoryStream(b);

        try
        {
            return (T)bf.Deserialize(ms);
        }
        finally
        {
            ms.Close();
        }
    }

This is the object I am trying to serialize:

[Serializable()]
class MatrixSerializable : ISerializable
{
    private bool markerFound;
    private Matrix matrix;

    public MatrixSerializable( Matrix m, bool b)
    {
        matrix = m;
        markerFound = b;
    }

    public MatrixSerializable(SerializationInfo info, StreamingContext ctxt)
    {
        markerFound = (bool)info.GetValue("markerFound", typeof(bool));

        matrix = Matrix.Identity;

        if (markerFound)
        {

            //deserialization code
        }
    }

    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        // serialization code
    }

    public Matrix Matrix
    {
        get { return matrix; }
        set { matrix = value; }
    }

    public bool MarkerFound
    {
        get { return markerFound; }
        set { markerFound = value; }
    }
}

And an example of how am running it:

        MatrixSerializable ms = new MatrixSerializable(Matrix.Identity * 5, true);

        string s = Serializer.SerializeObject<MatrixSerializable>(ms);

        Console.WriteLine("serialized: " + s);

        ms = Serializer.DeserializeObject<MatrixSerializable>(s);

        Console.WriteLine("deserialized: " + ms.Matrix + " " + ms.MarkerFound);

When I try to run this I get an error “SerializationException was unhandled: The input stream is not a valid binary format. The starting contents (in bytes) are: 41-41-45-41-41-41-44-2F-2F-2F-2F-2F-41-51-41-41-41 …”

Any advice on what I am doing wrong or how to fix this would be greatly appreciated!

  • 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-06-02T23:15:10+00:00Added an answer on June 2, 2026 at 11:15 pm

    You are using Base64 to convert byte array to string and GetUtf8 bytes to convert from string back to byte array.

    Replace System.Text.Encoding.UTF8.GetBytes(str); with Convert.FromBase64String(str);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm playing with PhoneGap for the first time. I'd like to do the following:
This is my first time playing with both ajax and objects, so please go
http://removed.com/jquery/# First time playing with jQuery, and kind of stuck already. I'm expecting the
Just playing around with memcache for the first time; here's my code: $memcache =
I am playing around with ASP.NET MVC for the first time, so I apologize
Playing with jquery for the first time, and I'm trying to get a simple
This is my first time playing around with linq and I can't quite figure
This is my first time playing with filters, so forgive me if this seems
This is the first time I'm playing with Monad Transformers. This is a simple
I'm playing around with Backbone.js for the first time, and I'm having a bit

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.