管理

VB6各数据类型序列化和反序列化

Posted on 2008-12-27 09:28  lzhdim  阅读(1083)  评论(1编辑  收藏  举报

Private Sub Command1_Click()

'序列化
Dim var1 As Byte
Dim var2 As Boolean
Dim var3 As Integer
Dim var4 As Long
Dim var5 As Single
Dim var6 As Double
Dim var7 As String

var1 = CByte(Text1.Text)
var2 = CBool(Text2.Text)
var3 = CInt(Text3.Text)
var4 = CLng(Text4.Text)
var5 = CSng(Text5.Text)
var6 = CDbl(Text6.Text)
var7 = Text7.Text

Open "c:"aaa.txt" For Binary As #1
Put #1, , var1
Put #1, , var2
Put #1, , var3
Put #1, , var4
Put #1, , var5
Put #1, , var6
Put #1, , var7
Close #1
End Sub

 

Private Sub Command2_Click()

'反序列化
Dim var1 As Byte   '1
Dim var2 As Boolean '2
Dim var3 As Integer '2
Dim var4 As Long '4
Dim var5 As Single '4
Dim var6 As Double '8
Dim var7 As String '变长

Open "c:"aaa.txt" For Binary As #1
Get #1, , var1
Get #1, , var2
Get #1, , var3
Get #1, , var4
Get #1, , var5
Get #1, , var6
var7 = Space(LOF(1) - 21) '变长字符串长度=文件总长度-前面各类型所占长度
Get #1, , var7
Close #1

Label1.Caption = CStr(var1)
Label2.Caption = CStr(var2)
Label3.Caption = CStr(var3)
Label4.Caption = CStr(var4)
Label5.Caption = CStr(var5)
Label6.Caption = CStr(var6)
Label7.Caption = CStr(var7)

End Sub

 

Private Sub Command3_Click()
    '数组序列化和反序列化
    Dim arr()     As Single
    Dim arr1()    As Single
    Dim i     As Long
    Dim j     As Long
    Dim h As Long
    Dim l As Long
    h = 1
    l = 2
    ReDim arr(h, l)
    For i = 0 To l
      For j = 0 To h
            arr(j, i) = 100.1
      Next j
    Next i
   
    Open "c:"aaa.txt" For Binary As #1
    Put #1, , arr
    Close #1
     
    ReDim arr1(h, l)
    Open "c:"aaa.txt" For Binary As #1
    Get #1, , arr1
    Close #1
End Sub

Copyright © 2000-2022 Lzhdim Technology Software All Rights Reserved