c# class to byte array

// Convert an object to a byte array
private byte[] ObjectToByteArray(Object obj)
{
    if(obj == null)
        return null;

    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    bf.Serialize(ms, obj);

    return ms.ToArray();
}

// Convert a byte array to an Object
private Object ByteArrayToObject(byte[] arrBytes)
{
    MemoryStream memStream = new MemoryStream();
    BinaryFormatter binForm = new BinaryFormatter();
    memStream.Write(arrBytes, 0, arrBytes.Length);
    memStream.Seek(0, SeekOrigin.Begin);
    Object obj = (Object) binForm.Deserialize(memStream);

    return obj;
}

4
2
Klaus Aumann 110 points

                                    
private byte[] ObjectToByteArray(object obj)
{
  // proper way to serialize object
  var objToString = System.Text.Json.JsonSerializer.Serialize(obj);
  // convert that that to string with ascii you can chose what ever encoding want
  return System.Text.Encoding.ASCII.GetBytes(objToString);
}

private object ByteArrayToObject(byte[] bytes)
{
  // make sure you use same type what you use chose during conversation
  var stringObj = System.Text.Encoding.ASCII.GetString(bytes);
  // proper way to Deserialize object
  return System.Text.Json.JsonSerializer.Deserialize<object>(stringObj));
}

4 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
convert string of bytes to byte array c# string array to byte array c# convert model to byte array c# how to get byte array from object c# c# how to convert an object to byte array c# byte array convert int convert int array to byte array c# c# array to byte array c# array to bytearray c# convert class object to byte array how to convert an object to byte array c# output byte array as numbers c# c# struct to byte array convert object to byte array c# .net 2.00 c# byte to array c# convert object to byte array 2020 object array to byte array c# convert array to byte array c# array to byte array c# c# convert string to byte array convert class object to byte array c# c# int to byte array copy byte[] to array c# class object to byte array c# c# convert int array to byte array int array to byte array c# c# convert object to byte array c# convert integer array to byte array c# convert object to bytearray model to byte array c# uint to byte array c# byte array to int array c# string as byte array c# convert byte array to class object in c# how to create byte array from an object in c# asndata to byte array c# string to byte array c# c# string to byte array convert object into byte array c# c# convert string array to byte array c# convert class into byte array cannot convert string to byte array c# ushort to byte array c# dictionary to byte array c# struct byte array to string c# c# run byte array exe serialize byte array to c# object byte array into c# object ISerializable c# web http image to byte array c# example byte array byte array c# c# byte to byte array convert object to byte array c# core 3.1 c# object for byte arrays convert data table to byte array c# byte array to protobuf c# convert byte to byte array c# file to byte array c# convert object to byte array c# c# serialize object to byte array c# cast class to byte array c# convert class to byte array c# class to byte array only values c# class to bytes c# from model to byte array convert model to byte c# Class to byte array c# convert objects to byte array c# c# object to byte array c# byte array to class instance object to byte array c# c# GetBytes from object C# save object as bytes c# convert object of byte to byte c# conver tobect to byte c# class to byte array
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source