I
I
Ingvar Von Bjork2016-01-10 23:24:01
Arrays
Ingvar Von Bjork, 2016-01-10 23:24:01

How to concatenate and disjoin byte[]?

One program sends a byte[] stream over the network that contains text and an image. The question is: how to properly combine the text and the image into this byte[] so that they can then be isolated? Perhaps the option is suitable, say, writing to the first 1024 bytes of text in the remaining 1024 pictures? But how to implement it correctly?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Vitaly Pukhov, 2016-01-11
@DeboshiR

I agree with Alexander Zaitsev , definitely serialization. Below is an example, honestly from here .

using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO; 
 
namespace Serialization
{
    [Serializable]
    class MSG
    {
        public int type;
        public string message;
        
        public MSG(int i, string s)
        {
            type = i;
            message = s;
        }
    }
    
    class Program
    {
        public static byte[] Serialization (MSG obj)
        {
            BinaryFormatter formatter = new BinaryFormatter();          
            MemoryStream stream = new MemoryStream();           
            formatter.Serialize(stream, obj);           
            byte[] msg = stream.ToArray();
            return msg;
        }
        
        public static MSG DeSerialization (byte[] serializedAsBytes)
        {
            MemoryStream stream = new MemoryStream();           
            BinaryFormatter formatter = new BinaryFormatter();          
            stream.Write(serializedAsBytes, 0, serializedAsBytes.Length);           
            stream.Seek(0, SeekOrigin.Begin);           
            return (MSG)formatter.Deserialize(stream); 
        }
        
        public static void Main(string[] args)
        {
            MSG msg = new MSG(1,"Я не сериализован");
            byte[] buf = new byte[1024];
            
            buf = Serialization(msg);
            
            Console.WriteLine("До сериализации  " + buf);
            
            MSG msg1 = new MSG(2,"Текст");
            msg1 = DeSerialization (buf);
            Console.WriteLine("После сериализации  " + msg1.message);
            
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}

A
Alexander Zaitsev, 2016-01-11
@nithrous

I would do it with serialization (BinaryFormatter class).
We create a class that contains an image and text, shove your image and text there, serialize it all using BinaryFormatter.Serialize( ) into a MemoryStream, and get an array of bytes from it.
Upon receiving an array of bytes, we create a MemoryStream from the array, deserialize from it using BinaryFormatter.Deserialize( ) - we get a class with our picture and text

P
Peter, 2016-01-10
@petermzg

The first 4 bytes are the size of the picture, then all the bytes of the picture, then 4 bytes are the size of the text, then the bytes of the text

D
Deerenaros, 2016-01-11
@Deerenaros

Strings can be written null-terminated. First
after Array.CopyTo and reverse conversion is

do {/*сейчас текст*/} while (str[i] != 0);
/*теперь остальное*/

Don't forget about coding. And to make it beautiful, you should listen to Alexander Zaitsev and implement ISerializable .

I
Ingvar Von Björk, 2016-01-11
@DeboshiR

Slightly corrected sources from Vitaly Pukhov :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Serialization
{
    [Serializable]
    class MSG
    {
        public int type;
        public string message;

        public MSG(int i, string s)
        {
            type = i;
            message = s;
        }
    }

    class Program
    {
        public static byte[] Serialization(MSG obj)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();
            formatter.Serialize(stream, obj);
            byte[] msg = stream.ToArray();
            return msg;
        }

        public static MSG DeSerialization(byte[] serializedAsBytes)
        {
            MemoryStream stream = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            stream.Write(serializedAsBytes, 0, serializedAsBytes.Length);
            stream.Seek(0, SeekOrigin.Begin);
            return (MSG)formatter.Deserialize(stream);
        }

        public static void Main(string[] args)
        {
            MSG msg = new MSG(65456541, "Я нasdsadsadsdafvdsafvbе сериализован");
            MSG msg1;
            byte[] buf = new byte[1024];

            buf = Serialization(msg);
            Console.WriteLine("До сериализации  " + msg.type + "|" + msg.message);

            msg1 = DeSerialization(buf);
            Console.WriteLine("После сериализации  " + msg1.type + "|" + msg1.message);

            Console.ReadKey(true);
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question