0

I have a struct which has some fields. I need a method to convert these fields to a byte array. There is a method, which I can use (" ObjectToByteArray"). I want to know how to use it. Is my code Correct??

public struct Convertor
{
    public string license { get; set; }
    public int Software { get; set; }
    public int StartDate { get; set; }
    public int EndDate { get; set; }
    public byte Count { get; set; }
    public int[] ActionCode { get; set; }

    public byte[] ConvertToArray()
    {
       var Result=new Convertor();

       return Result.ObjectToByteArray();
    }
}
7
  • You are looking for the very Broad Topic "Serialization". Commented Mar 25, 2018 at 4:46
  • 1
    @JPVenson - Specifically binary serialization. Unless you want to manually evoke BitConverter.GetBytes() for each field manually. Commented Mar 25, 2018 at 4:52
  • @ja72 for sure ;-). Still far to broad question. Commented Mar 25, 2018 at 4:54
  • @ja72 even if you do that, that its still "Serialization" ... pardon "Binary Serialization" ;-) Commented Mar 25, 2018 at 4:55
  • 1
    did you see this answer: stackoverflow.com/a/10502856/3668866 Commented Mar 25, 2018 at 5:02

1 Answer 1

1

Here is an example using System.Runtime.Serialization.Formatters.Binary.BinaryFormatter and the [Serializable()] attribute for the structure.

[Serializable()]
public struct Convertor 
{
    public string License { get; set; }
    public int Software { get; set; }
    public int StartDate { get; set; }
    public int EndDate { get; set; }
    public byte Count { get; set; }
    public int[] ActionCode { get; set; }

    public byte[] ConvertToArray()
    {
        var bf = new BinaryFormatter();
        using (var mem = new MemoryStream())
        {
            bf.Serialize(mem, this);
            return mem.ToArray();
        }
    }

    public static Convertor ConvertFromArray(byte[] buffer)
    {
        var bf = new BinaryFormatter();
        using (var mem = new MemoryStream(buffer))
        {
            return (Convertor)bf.Deserialize(mem);
        }
    }

    /// <summary>
    /// Checks for equality among <see cref="Convertor"/> classes
    /// </summary>
    /// <param name="other">The other <see cref="Convertor"/> to compare it to</param>
    /// <returns>True if equal</returns>
    public bool Equals(Convertor other)
    {
        return License == other.License
            && Software == other.Software
            && StartDate == other.StartDate
            && EndDate == other.EndDate
            && Count == other.Count
            && ActionCode.SequenceEqual(other.ActionCode);
    }

}
class Program
{
    static void Main(string[] args)
    {
        // Create a new object and add some data to it
        var a = new Convertor()
        {
            License = "ABC001",
            Software = 1,
            StartDate = 2018,
            EndDate = 2019,
            Count = 16,
            ActionCode = new[] { 67, 79, 68, 69, 49, 50, 51 }
        };

        // Serialize the object into a byte array
        var buffer = a.ConvertToArray();

        // Deserialize a new object from the byte array
        var b = Convertor.ConvertFromArray(buffer);

        // Check for equality
        var ok = a.Equals(b); // ok = true
    }
}
Sign up to request clarification or add additional context in comments.

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.