C++/CLI Code Snippet - Object to byte array

C++/CLI Code Snippet - Object to byte array

C++/CLI Code Snippet - Object to byte array

C++/CLI code snippet convert object to byte array. This method useful to store binary data in files. This function can be use to convert any object to byte array and store them in database.

Bookmark:

C++/CLI Code Snippet - Object to byte array

This .Net C++/CLI code snippet convert object to byte array. This method useful to store binary data in files. Most common method to store binary data in database is as a byte array format. This function can be use to convert any object to byte array and store them in database. This function uses System.IO and System.Runtime.Serialization.Formatters.Binary name spaces to convert object to byte array. Modify the exception handling section as to your project requirements.

Data that is serialized is easy to represent, transport, and store. Serialized data is naturally represented by byte arrays. Byte arrays, accordingly, are very easy to manipulate programmatically. Transmitting a Byte array over network is straightforward; indeed, a send() call on a standard socket requires a Byte array as a parameter. Additionally, serialized data can then be stored. It is intuitive that storing a Byte array onto some type of disk storage is straightforward, since fundamentally these devices deal with sequences of bytes.

array<System::Byte> ^ObjectToByteArray(System::Object ^_Object)
{
    try
    {
        // create new memory stream
        System::IO::MemoryStream ^_MemoryStream = gcnew System::IO::MemoryStream();

        // create new BinaryFormatter
        System::Runtime::Serialization::Formatters::Binary::BinaryFormatter ^_BinaryFormatter = gcnew System::Runtime::Serialization::Formatters::Binary::BinaryFormatter();

        // Serializes an object, or graph of connected objects, to the given stream.
        _BinaryFormatter->Serialize(_MemoryStream, _Object);

        // convert stream to byte array and return
        return _MemoryStream->ToArray();
    }
    catch (Exception ^_Exception)
    {
        // Error
        Console::WriteLine("Exception caught in process: {0}", _Exception->ToString());
    }

    // Error occured, return null
    return nullptr;
}


Here is a simple example showing how to use above function (ObjectToByteArray). In this example we convert picturebox image object to byte array.

array<System::Byte> ^_ByteArray = ObjectToByteArray(pictureBox1->Image);


C++/CLI Keywords Used:

  • MemoryStream
  • BinaryFormatter
  • Serialize
  • byte
  • Exception

Code Snippet Information:

  • Applies To: Visual Studio, .Net, C++, CLI, Byte Array, Object to byte array, Binary Data, Serialization, BinaryFormatter, Store binary data in database
  • Programming Language : C++/CLI

External Resources:

Matt Ault :: June 18-2010 :: 05:52 AM

Thought this was just what I was looking for, although it doesn't seem to work if the object passed is an array, even if passed a byte array!?

Alejandro Muñoz :: May 26-2011 :: 04:12 PM

Very good but your work, I tried it in my project but I need to know if it works also with the data that a sqldatareader contains(this datareader is a binary data type extracted from sqlserver2005) I would be grateful if you answer this...

Leave a comment