Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Conversion of Structure to Byte Array

Conversion of Structure to Byte Array

Scheduled Pinned Locked Moved C#
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lnmca
    wrote on last edited by
    #1

    Hi All, I am sending a structure object to another machine through TCP/IP by connecting through socket,so that i have to convert this object into byte array and also viceversa... Please help me,it is urgent. Regards, Lalit Narayan

    S 1 Reply Last reply
    0
    • L lnmca

      Hi All, I am sending a structure object to another machine through TCP/IP by connecting through socket,so that i have to convert this object into byte array and also viceversa... Please help me,it is urgent. Regards, Lalit Narayan

      S Offline
      S Offline
      satsumatable
      wrote on last edited by
      #2

      / 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;
      }

      [Serializable] attribute need to be enable serialization

      L 1 Reply Last reply
      0
      • S satsumatable

        / 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;
        }

        [Serializable] attribute need to be enable serialization

        L Offline
        L Offline
        lnmca
        wrote on last edited by
        #3

        Hi Thanks for ur reply...... When i am deserializing the object from bytes array,then i am getting error "Unable to find assembly 'DeviceApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'." Please give any solution for that. Regards, Lalit Narayan

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups