0

I am looking for a way to fast and simple implementation of this paradigm:

MyByteArray mb = new MyByteArray();
mb.Add<byte>(bytevalue);
mb.Add<float>(floatvalue);
mb.Add<string>(str);
mb.Add<MyClass>(object);

And then get byte[] from mb to send it as a byte packet via RPC call (to be decoded on the other side using the same technique).

I've found MemoryStream, but it looks like too overheaded for this simple operation.

Can you help me? Thank you.

3 Answers 3

2

What are you looking for is BinaryWritter. But it still needs a Stream to write on for pure logic reason. And the only Stream that fits in your need is MemoryStream.

Are you afraid of performance overhead ? You can create your MemoryStream from an existing byte array ;

    byte [] buffer = new byte[1024];
    using (var memoryStream = new MemoryStream(buffer))
    {
        using (var binaryWriter = new BinaryWriter(memoryStream))
        {
            binaryWriter.Write(1.2F); // float
            binaryWriter.Write(1.9D); // double
            binaryWriter.Write(1); // integer
            binaryWriter.Write("str"); // string
        }
    }
    // buffer is filled with your data now. 
Sign up to request clarification or add additional context in comments.

Comments

0

A tricky way to achieve this is to use a combination of builtin class in .net

 class Program
    {
        static void Main(string[] args)
        {    
            Program  program   =  new Program(); 
            var listBytes  = new List<byte>();
               listBytes.Add( program.CastToBytes("test")); 
               listBytes.Add(program.CastToBytes(5));         
        }

Note for a custom object you have to define an implicit operator on how the properties or all the object should be converted

        public byte[] CastToBytes<T>(T value)            
        {
            //this will cover most  of primitive types 

            if (typeof(T).IsValueType)
            {
                return BitConverter.GetBytes((dynamic)value);
            }

            if (typeof(T) == typeof(string))
            {
                return Encoding.UTF8.GetBytes((dynamic) value); 
            }
            //for a custom  object  you  have to  define  the rules 
            else
            {
                var formatter = new BinaryFormatter();
                var memoryStream = new MemoryStream();
                formatter.Serialize(memoryStream, value);
                return  memoryStream.GetBuffer();     
            }                                    
        }

    }

Comments

0

This looks like the case for Protocol Buffers, you could look at at protobuf-net.

First, let's decorate the classes.

[ProtoContract]
class User
{
  [ProtoMember(1)]
  public int Id { get; set; }

  [ProtoMember(2)]
  public string Name { get; set; }
}

[ProtoContract]
class Message
{
  [ProtoMember(1)]
  public byte Type { get; set; }

  [ProtoMember(2)]
  public float Value { get; set; }

  [ProtoMember(3)]
  public User Sender { get; set; }
}

Then we create our message.

var msg = new Message
{
  Type = 1,
  Value = 1.1f,
  Sender = new User
  {
    Id = 8,
    Name = "user"
  }
};

And now, we can use ProtoBuf's serializer to do all our work.

// memory
using (var mem = new MemoryStream())
{
  Serializer.Serialize<Message>(mem, msg);
  var bytes = mem.GetBuffer();
}

// file
using (var file = File.Create("message.bin")) Serializer.Serialize<Message>(file, msg);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.