Convert an object to a byte array in C#, send it over a socket, then convert back into object


So I have a server and a client that communicate various data back
and fourth. Initially I had a complicated method that went through the
byte array and converted all of its variables and strings, one by one,
into what they were supposed to be. I learned I could put all of the
variables into an object and convert it to a byte array using

/>private static byte[] ObjectToByteArray2(Object obj)

{
if (obj == null)
return null;

BinaryFormatter bf = new BinaryFormatter();

MemoryStream ms = new MemoryStream();
bf.Serialize(ms,
obj);
return ms.ToArray();
}

Hi I can’t find sample for convert MULTI PAGE tiff image to byte
array.


For convert byte array to Tiff I use this
method


public static Tiff
CreateTiffFromBytes(byte[] bytes)
{
using (var
ms = new MemoryStream(bytes))
{
Tiff
tiff = Tiff.ClientOpen("in-memory", "r", ms, new TiffStream());

return tiff;
}
}
/>

EDITED:


This method convert TIFF image with more
pages to byte Array. I think in this method will be root of
problem.


//imageIn is tif image with 12 pages

publ
Programming Languages

So I have a server and a client that communicate various data back
and fourth. Initially I had a complicated method that went through the
byte array and converted all of its variables and strings, one by one,
into what they were supposed to be. I learned I could put all of the
variables into an object and convert it to a byte array using

/>private static byte[] ObjectToByteArray2(Object obj)

{
if (obj == null)
return null;

BinaryFormatter bf = new BinaryFormatter();

MemoryStream ms = new MemoryStream();
bf.Serialize(ms,
obj);
return ms.ToArray();
}
C & C++ & C#
Hi All,
Basically my situation is this, I have a server
implemented in C++,
unmanaged code, using proprietery protocol
over TCP/IP to communicate
with the cilent(c++ also).
Now, I
am implementing the another client in C#. Server side can not />be
changed :(
So, I am using tcp/ip sockets. In the end I
get byte[] on the client
side, which originally was some C++
class object, converted to byte
array.
What is the most
efficient way to convert that byte array to C#
object?
Ive
checked few sources, writing custom serializer doesnt seem to be />a good idea, as the serialize will receive a stream object, which
only
C & C++ & C#
I am wondering if its possible to convert an object or a struct to a
byte
array of the raw data. Its been a long time, but I seem to
remember doing
this in C++ at one point, and with very little
code. What Id like to be
able to do is save a struct into a
registry binary value.
Thanks in advance,
Chris LaJoie

Nov 15 05
#1 Post Reply Share this Question 1
Reply (adsbygoogle = window.adsbygoogle || []).push({});
P: n/a
Development
i have a program (VB6.0) which runs fine, it called a ocx and it
return a 3 bytes string.
now, i rewrite the program in VB.net,
and again the ocx return as Object type
firstly, i try this: />dim x as String = ocx.get_field("id")
which return x
as a string with 1 character only. what i expect is 3 characters />then, i try to do as follows:
dim x as Object =
ocx.get_field("id")
dim b as Byte() = CType(x,
Byte())
which results in InvalidCastException.
Then, i try
in this way:
Dim b As Byte() =
System.Text.Encoding.Unicode.GetBytes(ocx.g
Programming Languages
I have a list of object with different types such as Int32, ushort,
ulong etc..
I need to convert all of them to a byte array. />For ex:
object[] = {(Int32)10, (ushort)8,
(ulong)0x11223344};
I want to convert this to byte[] of values.
How to achieve this?
Programming Languages

- Technology - Languages
+ Webmasters
+ Development
+ Development Tools
+ Internet
+ Mobile Programming
+ Linux
+ Unix
+ Apple
+ Ubuntu
+ Mobile & Tablets
+ Databases
+ Android
+ Network & Servers
+ Operating Systems
+ Coding
+ Design Software
+ Web Development
+ Game Development
+ Access
+ Excel
+ Web Design
+ Web Hosting
+ Web Site Reviews
+ Domain Name
+ Information Security
+ Software
+ Computers
+ Electronics
+ Hardware
+ Windows
+ PHP
+ ASP/ASP.Net
+ C/C++/C#
+ VB/VB.Net
+ JAVA
+ Javascript
+ Programming
Privacy Policy - Copyrights Notice - Feedback - Report Violation 2018 © BigHow