Serialize/Deserialize two-dimensional array

不羁岁月 提交于 2019-12-22 05:16:16

问题


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

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

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

I then save byArr to the Memo field.

Please provide sample code.


回答1:


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)

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.




回答2:


I changed the data type of the Access field from Memo to OLE Object and that seems to work. When I look at the data in Access it tells me the field is "Long binary data" and I was able to use the following code after reading the record from Access:

Dim f As New System.Runtime.Serialization.Formatters.BinaryFormatter
Dim byArr As Byte()
byArr = DirectCast(dtLHS.Rows(0).Item("LHS"), Byte())
Dim theArrayAsString As String = Convert.ToBase64String(byArr)
Dim ms2 As New MemoryStream(Convert.FromBase64String(theArrayAsString))
Dim newLHS(,) as single = f.Deserialize(ms2)

Is this correct?



来源:https://stackoverflow.com/questions/16282748/serialize-deserialize-two-dimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!