Click here to Skip to main content
Email Password   helpLost your password?

Introduction

this dll help users to convert between any object and byte array which will be very useful when using to insert objects into a DataBase.(specialy images)

it contain two main methods

ObjectToByteArray:

       which accept an object and return array of bytes .

ByteArrayToObject:

      which accept an array of bytes and return object.

why I make this dll?

I need in a project on automatic exam system in my work (Scientific calculation Center,Mansoura University,Egypt) to insert into database an custum object of exam data.

I'm sure that I can find a solution to this problem by add Exam Details table to my dataBase

but I hope to  use the first strategy ,So I make it.

keywords

Database,converter,image,byte array

      

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionProblem while converting
Muneswar
23:18 3 Oct '07  
Hi,

I tried to use the function ByteArrayToObject and it is returning null object. I tried to debug the code and found that the statement return formatter.Deserialize(stream); is throwing the exception called "End of Stream encountered before parsing was completed." and so returning the null object.

Below is the code which calls the function.

Byte[] ar = new Byte[3]{1,2,3};
Object o1;
o1 = ObjectByteArrayConverter.ByteArrayToObject(ar);

Please let me know the problem which would be a great help.

Thanks in advance,


Muneswar Rao. G
Generalproblem with some objects
jag_Dalton
3:00 22 Feb '07  
Hello

Nice example.
But I tried to convert a xmlelement to byte Array.
But without success.

VS is tolding me that System.Xml.XmlElement ist not serialisable.

Why? And what can I do to solve it?

Thanks a lot
Jag
GeneralRe: problem with some objects
DrPhilz
2:55 28 Jun '07  
You have to tell .NET that your object is serializable. To do this add this to your class :

Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization

<Serializable()> _
Public Class MyFisrtClass

...

End class
GeneralHuge problem with the methods
Kasracer
2:38 26 Jan '06  
Take this example.

When using encryption in .Net, you use mostly byte arrays. Now if you want to encrypt an object using your methods, you'd first take the object and serialize it (thus, converting it to a byte[]). Now you pass that to your encryption routines and you're done.

Now, let's say you add the result to an ArrayList, which automatically puts the byte[] into a System.Object.

What do you do when you want to decrypt it? You have to have it in byte[] from. If you attempt to use your ObjectToByteArray method, it'll attempt to serialize it again thus making it practically impossible to ever get the original data.

You can try to cast the Object to a byte[], which sometimes works (I've done many tests) but it isn't 100%.

So these methods will work with some things, but they are not very reliable especially for encryption.

Also, please use the using statements with any variable that has an IDisposable base. That way everything is cleaned up immediately and you don't even need a finally statement.

BinaryIdiot.com - great source for news and software.
GeneralRe: Huge problem with the methods
jigar_dotnet
3:13 4 Apr '07  
i have also encounter the problem while using encryption on object.

how to encrypt a object and how to decrypt it later on .???
please send the code if u have..
QuestionConvert from object to byte array problem
analyzer2
10:48 19 Jan '06  
Hello

I use the dll to convert a object to a byte array. Unfortunately there is a problem. The value of the object isn't the same as the new value.

example: My object has 248 values [0] 60
Convert to a byte array for example the values is [0] 0
The lenght is greater than befor.

Do you know this problem?
Thanks
Regards
analyzer
AnswerRe: Convert from object to byte array problem
Kasracer
2:40 26 Jan '06  
I'm confused. Are you saying it's larger as a byte[] than an object or something else? if it's larger then that's no surprise because it's serializing the data which means it also adds meta data because normally serialization is used with Files.

BinaryIdiot.com - great source for news and software.
GeneralRe: Convert from object to byte array problem
analyzer2
2:55 26 Jan '06  
The array is lengther than the orginal object.
GeneralRe: Convert from object to byte array problem
Kasracer
0:37 31 Jan '06  
Sorry for the late reply.

This is because the author serializes the data, which adds NTFS metadata to it. So no matter what, any Object to byte[] conversion with this code will be larger.

BinaryIdiot.com - great source for news and software.
GeneralRe: Convert from object to byte array problem
analyzer2
1:25 31 Jan '06  
Thanks, unfortunately that's a problem for my application. I'll search an other possibility for my problem.
GeneralRe: Convert from object to byte array problem
donjefe
15:54 3 Aug '06  
Try stripping the first 4 bytes from the byte array when you return it. I had this problem when using custom resources in resource files.
Generalnice one!
CodeGimp
0:23 27 Jun '05  
A really useful little code snippet! Thanks!
GeneralArticle justified?
jmw
3:16 18 Jun '04  
I think this is more of a code-snippet than a project.. so I'm not really sure that this article is justified. Perhaps if you explained some of the other aspects serialization, such as what the formatter is for and perhaps a bit about other formatters... maybe then it would be a more useful article.

Plus, in the code, I don't like the way you're catching all serialization exceptions in the serializer code and just returning null. I think you'd want to know why your object couldn't be serialized.
GeneralRe: Article justified?
Tamer Ahmed Farrag
3:47 18 Jun '04  
thankx for your interest, yes I use this dll in many other projects I will try to provide an example of its usage.
in my project I don't cear with the error Knowing , but It a good note
GeneralI update the article
Tamer Ahmed Farrag
0:54 18 Jun '04  
Wink
Thankx mav.northwind ,

really, I don't notice this constructor (may be I was sleep)Sleepy

I modify the article .and Update It.


GeneralWhy this way?
mav.northwind
21:19 17 Jun '04  
Hi!
Is there a reason why you use a MemoryStream for decoding but actually write to file for encoding?
Not only is this a huge performance loss but you also use a fixed (relative OMG ) file name (=>bad design) and do not clean up after yourself.

I really think you should clean up the class...

Regards,
mav
GeneralRe: Why this way?
Tamer Ahmed Farrag
22:42 17 Jun '04  
thankx for your message I have a reason to do that

when I use MemoryStream I must declare an array of bytes with fixed length .So there is a limitation may be occured when use this class with big object.
The remark of deleting file is very good . I will do it (I forget it)

I hope If u can help me

GeneralRe: Why this way?
mav.northwind
23:08 17 Jun '04  
Tamer, you don't need to give a fixed length in the constructor of the MemoryStream.
Take a look at this MemoryStream Constructor.

Regards,
mav
GeneralRe: Why this way?
jmw
3:11 18 Jun '04  
Incidentally it's worth setting the Capacity of the memory stream to the Position right before you run GetBuffer, otherwise you end up with extra unnecessary padding in the buffer.
GeneralRe: Why this way?
Mihies
4:28 19 Jun '04  
Or just use GetBuffer and use only a subset of returned data.

Miha Markic [MVP C#]/RightHand .Net consulting and software development
GeneralRe: Why this way?
jmw
5:16 19 Jun '04  
Right, and Position tells you the boundary of the data written so far (it's difficult to determine that otherwise). Setting Capacity determines the size of the buffer returned by GetBuffer(), so it saves that extra allocation and copying that you're suggesting.


Last Updated 18 Jun 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010