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 9250037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:21:01+00:00 2026-06-18T10:21:01+00:00

I use protobuf-net for Serializing a structure, but it returns an empty array. public

  • 0

I use “protobuf-net” for Serializing a structure, but it returns an empty array.

public static byte[] PacketToArray(Packet packet)
{
    IFormatter formatter = new BinaryFormatter();
    MemoryStream stream = new MemoryStream();

    Serializer.Serialize(stream, packet);
    byte[] packetArray = stream.GetBuffer();

    stream.Close();

    return packetArray;
}

packetArray[] ist at the ending “{byte[0]}” but there should be some data in.
The data of “packet” is:

[ProtoContract]
public struct Packet
{
    [ProtoMember(1)] 
    public int opcode;

    [ProtoMember(2)] 
    public string message;
}

And in the testings the values vor opcode is 0 and for message its null.
Where is the Problem?

  • 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-18T10:21:02+00:00Added an answer on June 18, 2026 at 10:21 am

    And in the testings the values vor opcode is 0 and for message its null. Where is the Problem?

    What makes you think there is a problem? 0 bytes is perfectly legal for protobuf-net, and is expected in this case as there is nothing interesting to serialize; it can deserialize a 0 and a null without needing any external data. The key point here: if you deserialize that same zero bytes, you will get back a Packet with a 0 opcode and a null message. Job done.

    If you want it to handle “framing” so that multiple messages can be read separately, then use SerializeWithLengthPrefix (and DeserializeWithLengthPrefix), but: no problem here.

    Actually, there is a bug in your code, though:

    byte[] packetArray = stream.GetBuffer();
    

    If you use GetBuffer() without also tracking the .Length, you will get the oversized backing buffer, which contains garbage (in this case zeros). Use ToArray() instead. So:

    public static byte[] PacketToArray(Packet packet) {
        using(MemoryStream stream = new MemoryStream()) {
            Serializer.Serialize(stream, packet); // or SerializeWithLengthPrefix
            return stream.ToArray();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the RedisConnection Set method to set the byte array but how do
I have been trying to use protobuf-net with MonoTouch but I have no idea
Is it possible to use protobuf-net in RESTful webservices created using WCF RESTful starter
I have a WCF service hosted that is returning a byte[] of protobuf-net serialized
I'm trying to use protobuf in a C# project, using protobuf-net, and am wondering
Can I use net.tcp bindings for protobuf-net WCF? Can I use ClientBase or I
I have problem with deserialization using protobuf-net. I have the following class: [ProtoContract] public
C#, How can I use ProtoBuf .NET to serialize a dataTable? Can I please
I'm trying to move an existing code to use ProtoBuf-Net. Some classes have DataContract
I translate my project to use protobuf-net instead of BinaryFormatter. It looks like there

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.