如何有效地将字符串、整数、双精度、日期时间转换为二进制,反之亦然?
我对 C# 还很陌生(我正在使用 .NET 4.0),所以请耐心等待。我需要将一些对象属性(它们的属性是 int、double、String、boolean、datetime)保存到文件中。但我想使用自己的加密方式对文件进行加密,因此无法使用 FileStream 转换为二进制。另外,由于性能问题,我不想使用对象序列化。这个想法很简单,首先我需要以某种方式将对象(它们的属性)转换为二进制(数组),然后加密(某种异或)数组并将其附加到文件末尾。首先读取时解密数组,然后以某种方式将二进制数组转换回对象属性(我将从中生成对象)。
我知道(大致=))如何手动转换这些东西,我可以对其进行编码,但它没有用(太慢)。我认为最好的方法就是获取内存中属性的表示并保存它。但我不知道如何使用 C# 来做到这一点(也许使用指针?)。我也考虑过使用 MemoryStream,但我再次认为它效率低下。我正在考虑类 Converter,但它不支持 toByte(datetime) (文档说它总是抛出异常)。
为了转换回来,我认为唯一的选择是类转换器。
注意:我知道对象的结构,它们不会改变,也知道最大字符串长度。
感谢您的所有想法和时间。
编辑:我将只存储对象的一部分,在某些情况下还存储不同对象的一部分(一个对象类型的几个属性和另一个对象类型的几个属性),因此我认为序列化不是我的选择。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 BinaryFormatter 对它们进行序列化,并在将它们写入 FileStream 之前对其进行加密。这将写入和读取一个对象(我使用 DateTime 作为示例)
如果您只是想要基本类型的“二进制”内容,您可以使用:
对于 DateTime,您保存 Tick 属性(如果您想要 Kind 属性) )。对于字符串,您使用 Encoding.UTF8.GetBytes。
You can serialize them with BinaryFormatter and, before writing them to the FileStream, encrypt them. This will write and read an object (I'm using a DateTime as an example)
If you simply want the "binary" content of a base type you can use:
For DateTime you save the Tick property (and if you want the Kind property). For string you use Encoding.UTF8.GetBytes.
有许多不同的方法可以做到这一点。也许最简单且性能应该足够的方法是将每个记录写入
MemoryStream
,获取结果字节数组,对该数组进行加密,然后使用 < code>FileStream 和一个BinaryWriter
。读回来很容易。您在文件上设置一个
BinaryReader
,调用ReadInt32
获取记录长度,调用Read(RecordBuffer, 0, length)
获取记录长度记录字节,进行解密,然后使用 MemoryStream 进行反序列化。There are many different ways to do this. Perhaps the easiest, and performance should be adequate, would be to write each record to a
MemoryStream
, get the resulting array of bytes, do your encryption on that array, and then write the array to file usingFileStream
and aBinaryWriter
.Reading that back in is quite easy. You set up a
BinaryReader
on the file, callReadInt32
to get the record length, callRead(RecordBuffer, 0, length)
to get the record bytes, do your decryption, and then use aMemoryStream
to de-serialize.我将其发布为答案,因为我太绿了,无法写评论。
像 Jens 和 Xanatos 所说的那样,从序列化整个类开始。很可能这会没问题。找出答案的最好方法就是尝试一下。
如果您确实需要使文件更小,则创建仅包含需要序列化的成员的新类。当您写入文件时,您可以将较大的对象转换为较小的格式并将其序列化,然后当您加载文件时,您可以再次从较小的对象初始化完整的对象。
I'm posting this as an answer since I'm too green to write comments.
Start out with serializing the entire classes like Jens and Xanatos said. Most likely this will be fine. The best way to find out is to try it.
If you really need to make the files smaller, then create new classes containing only the members that need to be serialized. When you go to write the file, you can convert your larger objects to the smaller format and serialize that, then when you load the file you can initialize the full objects from the smaller ones again.