How to efficiently convert String, integer, double, datetime to binary and vica versa?

Posted by Ben on Stack Overflow See other posts from Stack Overflow or by Ben
Published on 2011-02-19T14:57:24Z Indexed on 2011/02/19 15:25 UTC
Read the original article Hit count: 267

Filed under:
|
|
|

Hi,

I'm quite new to C# (I'm using .NET 4.0) so please bear with me. I need to save some object properties (their properties are int, double, String, boolean, datetime) to a file. But I want to encrypt the files using my own encryption, so I can't use FileStream to convert to binary. Also I don't want to use object serialization, because of performance issues. The idea is simple, first I need to somehow convert objects (their properties) to binary (array), then encrypt (some sort of xor) the array and append it to the end of the file. When reading first decrypt the array and then somehow convert the binary array back to object properties (from which I'll generate objects).

I know (roughly =) ) how to convert these things by hand and I could code it, but it would be useless (too slow). I think the best way would be just to get properties' representation in memory and save that. But I don't know how to do it using C# (maybe using pointers?). Also I though about using MemoryStream but again I think it would be inefficient. I am thinking about class Converter, but it does not support toByte(datetime) (documentation says it always throws exception).

For converting back I think the only options is class Converter.

Note: I know the structure of objects and they will not change, also the maximum String length is also known.

Thank you for all your ideas and time.

EDIT: I will be storing only parts of objects, in some cases also parts of different objects (a couple of properties from one object type and a couple from another), thus I think that serialization is not an option for me.

© Stack Overflow or respective owner

Related posts about c#

Related posts about binary