序列化/反序列化二维数组 [英] Serialize/Deserialize two-dimensional array

查看:206
本文介绍了序列化/反序列化二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我之前的问题被认为过于模糊.所以让我说得更具体一些.

For some reason my previous question was considered too vague. So let me be more specific.

我有一个单一类型的二维数组.我想将其序列化以保存在 Access 数据库中.

I have a 2 dimensional array of type single. I want to serialize it to save in an Access database.

建议将其保存为 Memo 字段,这很好.我想稍后读取 Memo 字段并将其反序列化以检索原始数组.我在网上和这里进行了广泛的搜索,但找不到答案.我相信我正确地序列化了数组,但不知道如何反序列化它.

The suggestion was to save it as a Memo field which is fine. I want to later read the Memo field and deserialize it to retrieve the original array. I have searched extensively on the web and here and can not find the answer. I believe I am serializing the array correctly but do not know how to deserialize it.

此代码似乎适用于序列化,但我不知道如何反序列化:

This code appears to work for serializing but I can not figure out how to deserialize:

Dim f As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter  
Dim ms As New MemoryStream  
f.Serialize(ms, arLHS)  
Dim byArr As Byte() = ms.ToArray

然后我将 byArr 保存到备注字段.

I then save byArr to the Memo field.

请提供示例代码.

推荐答案

你可以通过 base64 转换器反序列化:

You can deserialize it via base64 converter:

Dim str_b64 As String = Convert.ToBase64String(byArr)
Dim ms2 As New MemoryStream(Convert.FromBase64String(str_b64))
Dim intArr2(,) As Int32 = f.Deserialize(ms2)

这可能看起来有些尴尬,但它确实有效 - 在 VS 2010 的控制台应用程序中进行了测试.

This may look somewhat awkward, but it works - tested in a console app in VS 2010.

信用在这里.通过这个链接,你还可以找到完整版的代码来玩.

Credit goes here. Through this link, you can also find the full version of the code to play with.

这篇关于序列化/反序列化二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆