 |
|
 |
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
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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..
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
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.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
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.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thanks, unfortunately that's a problem for my application. I'll search an other possibility for my problem.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
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.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
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.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
 Thankx mav.northwind ,
really, I don't notice this constructor (may be I was sleep)
I modify the article .and Update It.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
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 ) file name (=>bad design) and do not clean up after yourself.
I really think you should clean up the class...
Regards, mav
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
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.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
Or just use GetBuffer and use only a subset of returned data.
Miha Markic [MVP C#]/RightHand .Net consulting and software development
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
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.
|
| Sign In·View Thread·PermaLink | 5.00/5 (2 votes) |
|
|
|
 |