Private Sub Command1_Click() Dim sj(1 To 4) As Byte Dim i As Integer sj(1) = &HB sj(2) = &HA sj(3) = &HC sj(4) = &H0 Open "C:\ts.dat" For Binary Access Write As #1 For i = 1 To 4 Put #1, i, sj(i) Next Close #1 End Sub
回二楼就是因为要做硬件上的东西,本人定义了一个结构, 'ÏÂÔØÊý¾Ý±íÃèÊö·û Type TS_Software_Descriptor Descriptor_Tag As Byte Descriptor_Length As Byte Software_Version As String * 16 Software_Size As String * 32 Software_CRC As String * 32 Last_Table_Id As String * 5 Reserved_Feature_Use3 As String * 3 End Type'TS·Ö¶Î Type TS_Data_Section Table_Id As Byte Section_Syntax_Indicator As String Reserved_Feature_Use3 As String * 3 Section_Length As String * 12 Reserved_Feature_Use13 As String * 13 Sub_Table_Id As String * 5 Reserved_Feature_Use2 As String * 2 Version_Number As String * 5 Current_Next_Indicator As String Section_Number As Byte Last_Section_Number As Byte Reserved_Feature_Use4 As String * 4 Descriptor_Loop_Length As String * 12 TS_Descriptor As TS_Software_Descriptor Payload_Data(0 To 3072) As Byte CRC32 As String * 32 End Type 现在.要将二进制数据,打包,但定义中的都是string类型不会转,存到扩展名为TS的二进制文件中去打包
Private Sub Command1_Click() Open App.Path + "\2.bak" For Binary As #2 Put #2, , 10 'дÈëÊ®Áù½øÖƵÄA Put #2, , 11 Put #2, , 12 Put #2, , 13 Put #2, , 14 Put #2, , strHexToNum("F") Put #2, , 15 Put #2, , 1 Put #2, , 2 Put #2, , 3 Put #2, , strHexToNum("4") Close #2 s = "ABC" MsgBox ".........." + TypeName(s) + ".........."
End Sub Function strHexToNum(s As String) As Integer Select Case s Case "A": strHexToNum = 10 Case "B": strHexToNum = 11 Case "C": strHexToNum = 12 Case "D": strHexToNum = 13 Case "E": strHexToNum = 14 Case "F": strHexToNum = 15 Case Else strHexToNum = CInt(s) End Select End Function 我自己写的一位可以写正常,两个例如"AB",就不行了
你的问题在BASIC语言中,是属于定义结构体数据类型,对文件进行随机读写。 请参阅http://zhidao.baidu.com/question/42391351.html http://zhidao.baidu.com/question/42391753.html 链接的答复,注意每个结构体的字节总长度。 Option Explicit Private Type www a As Long b As Single c As Single End Type Dim FileLength Dim nwww As www Dim recordnum As Integer Private Sub Command5_Click() ' 读记录 recordnum = InputBox("查询记录号", recordnum) Open "c:\s.dat" For Random As #1 Len = 12 Get #1, recordnum, nwww Close #1 Text5 = nwww.a & " " & nwww.b & " " & nwww.c End Sub Private Sub Command4_Click() ' 获取记录数 Open "c:\s.dat" For Random As #1 Len = 12 recordnum = LOF(1) \ Len(nwww) Text5 = recordnum Close #1 End Sub Private Sub Command1_Click() '追加记录 nwww.a = InputBox("输入nwww.a", nwww.a) nwww.b = InputBox("输入nwww.b", nwww.b) nwww.c = InputBox("输入nwww.c", nwww.c) Open "c:\s.dat" For Random As #1 Len = 12 recordnum = LOF(1) \ Len(nwww) Text5 = recordnum recordnum = recordnum + 1 Put #1, recordnum, nwww Close #1 End Sub
如果一次最多处理两个字符,可以这样,否则就要用数组了 Function strHexToNum(s As String) As Integer dim v as integer v = CInt("&H" & left$(s,1)) if len(s)>1 then v = v + CInt("&H" & Mid$(s,2,1)) * &H100 end if strHexToNum = v End Function
最后,大家可以帮看看,加分的问题 type aaa A as byte '这里只能为5个bit,但VB没有Bit B as long '25个bit c as byte '1个bit d as integer '因为是8个bit,我定义成了整型 f as byte '3 bit end type咋样才能将这个结构中的值连在一个Var的变量中,并且这个结构的长度我咋得出呀下边说明一下:我现在就卡在这了,其实上边的也是这样的,我要得出这个结构CRC32的值(这个我可以实现了),现在就是想求结构的长度(字节算),把这个结构中的所有变量连接在一起,我好计算CRC值(最重要的)
确定没写错,不是: type aaa A '5个bit B '15个bit c '1个bit d '8个bit f '3 bit end type
使用 Put 语句将数据写入文件中。示例中写入五个用户自定义数据类型 Record 的记录。Type Record ' 定义用户自定义数据类型。 ID As Integer Name As String * 20 End TypeDim MyRecord As Record, RecordNumber ' 声明变量。 ' 以随机访问方式打开文件。 Open "TESTFILE" For Random As #1 Len = Len(MyRecord) For RecordNumber = 1 To 5 ' 循环五次。 MyRecord.ID = RecordNumber ' 定义 ID。 MyRecord.Name = "My Name" & RecordNumber ' 建立字符串。 Put #1, RecordNumber, MyRecord ' 将记录写入文件中。 Next RecordNumber Close #1 ' 关闭文件。
Option Explicit Private Type TS_Software_Descriptor Descriptor_Tag As Byte Descriptor_Length As Byte Software_Version As String * 16 Software_Size As String * 32 Software_CRC As String * 32 Last_Table_Id As String * 5 Reserved_Feature_Use3 As String * 3 End Type Dim TS_Descriptor As TS_Software_Descriptor Private Type TS_Data_Section Table_Id As Byte Section_Syntax_Indicator As String * 1 '假设长度为1 Reserved_Feature_Use3 As String * 3 Section_Length As String * 12 Reserved_Feature_Use13 As String * 13 Sub_Table_Id As String * 5 Reserved_Feature_Use2 As String * 2 Version_Number As String * 5 Current_Next_Indicator As String * 1 '假设长度为1 Section_Number As Byte Last_Section_Number As Byte Reserved_Feature_Use4 As String * 4 Descriptor_Loop_Length As String * 12 TS_Descriptor(1 To 90) As Byte Payload_Data(0 To 3072) As Byte CRC32 As String * 32 End Type Dim ab As TS_Data_Section Private Sub Command1_Click() Print UBound(ab.Payload_Data) Print Len(ab.Section_Syntax_Indicator) Print Len(TS_Descriptor) Print Len(ab) End Sub
将下面的代码放在一个类中,Data() 就是将各个 Bit 串联后的字节数组,你可以用它来计算 CRC 值(有效位 42 Bit,存储用 6 Byte)。 Option ExplicitPrivate Data(0 To 5) As Byte'[0]: 0-4 Public Property Get A() As Byte A = Data(0) And &H1F End PropertyPublic Property Let A(ByVal RHS As Byte) Data(0) = (Data(0) And &HE0) Or (RHS And &H1F) End Property'[0]: 5-7, [1]:0-7, [2]:0-7, [3]:0-5 Public Property Get B() As Long B = (Data(0) \ &H20) Or _ (Data(1) * 8) Or _ (Data(2) * &H800&) Or _ (Data(3) And &H3F) * &H80000 End PropertyPublic Property Let B(ByVal RHS As Long) Data(0) = (Data(0) And &H1F) Or ((RHS And 7) * &H20) Data(1) = (RHS And &H7F8&) \ 8 Data(2) = (RHS And &H7F800) \ &H800& Data(3) = (Data(3) And &HC0) Or ((RHS And &H1F80000) \ &H80000) End Property'[3]:6 Public Property Get C() As Byte C = (Data(3) And &H40) \ &H40 End PropertyPublic Property Let C(ByVal RHS As Byte) Data(3) = (Data(3) And &HBF) Or ((RHS And 1) * &H40) End Property'[3]:7, [4]:0-6 Public Property Get D() As Byte D = (Data(3) \ &H80) Or _ ((Data(4) And &H7F) * 2) End PropertyPublic Property Let D(ByVal RHS As Byte) Data(3) = (Data(3) And &H7F) Or ((RHS And 1) * &H80) Data(4) = (Data(4) And &H80) Or (RHS \ 2) End Property'[4]:7, [5]:0-1 Public Property Get F() As Byte F = (Data(4) \ &H80) Or _ ((Data(5) And 3) * 2) End PropertyPublic Property Let F(ByVal RHS As Byte) Data(4) = (Data(4) And &H7F) Or ((RHS And 1) * &H80) Data(5) = (RHS And 6) \ 2 End Property
Private Sub Command1_Click()
Dim sj(1 To 4) As Byte
Dim i As Integer
sj(1) = &HB
sj(2) = &HA
sj(3) = &HC
sj(4) = &H0
Open "C:\ts.dat" For Binary Access Write As #1
For i = 1 To 4
Put #1, i, sj(i)
Next
Close #1
End Sub
'ÏÂÔØÊý¾Ý±íÃèÊö·û
Type TS_Software_Descriptor
Descriptor_Tag As Byte
Descriptor_Length As Byte
Software_Version As String * 16
Software_Size As String * 32
Software_CRC As String * 32
Last_Table_Id As String * 5
Reserved_Feature_Use3 As String * 3
End Type'TS·Ö¶Î
Type TS_Data_Section
Table_Id As Byte
Section_Syntax_Indicator As String
Reserved_Feature_Use3 As String * 3
Section_Length As String * 12
Reserved_Feature_Use13 As String * 13
Sub_Table_Id As String * 5
Reserved_Feature_Use2 As String * 2
Version_Number As String * 5
Current_Next_Indicator As String
Section_Number As Byte
Last_Section_Number As Byte
Reserved_Feature_Use4 As String * 4
Descriptor_Loop_Length As String * 12
TS_Descriptor As TS_Software_Descriptor
Payload_Data(0 To 3072) As Byte
CRC32 As String * 32
End Type
现在.要将二进制数据,打包,但定义中的都是string类型不会转,存到扩展名为TS的二进制文件中去打包
Put #2, , 10 'дÈëÊ®Áù½øÖƵÄA
Put #2, , 11
Put #2, , 12
Put #2, , 13
Put #2, , 14
Put #2, , strHexToNum("F")
Put #2, , 15
Put #2, , 1
Put #2, , 2
Put #2, , 3
Put #2, , strHexToNum("4")
Close #2
s = "ABC"
MsgBox ".........." + TypeName(s) + ".........."
End Sub
Function strHexToNum(s As String) As Integer
Select Case s
Case "A": strHexToNum = 10
Case "B": strHexToNum = 11
Case "C": strHexToNum = 12
Case "D": strHexToNum = 13
Case "E": strHexToNum = 14
Case "F": strHexToNum = 15
Case Else
strHexToNum = CInt(s)
End Select
End Function
我自己写的一位可以写正常,两个例如"AB",就不行了
请参阅http://zhidao.baidu.com/question/42391351.html
http://zhidao.baidu.com/question/42391753.html
链接的答复,注意每个结构体的字节总长度。
Option Explicit
Private Type www
a As Long
b As Single
c As Single
End Type
Dim FileLength
Dim nwww As www
Dim recordnum As Integer
Private Sub Command5_Click() ' 读记录
recordnum = InputBox("查询记录号", recordnum)
Open "c:\s.dat" For Random As #1 Len = 12
Get #1, recordnum, nwww
Close #1
Text5 = nwww.a & " " & nwww.b & " " & nwww.c End Sub Private Sub Command4_Click() ' 获取记录数
Open "c:\s.dat" For Random As #1 Len = 12
recordnum = LOF(1) \ Len(nwww)
Text5 = recordnum
Close #1
End Sub Private Sub Command1_Click() '追加记录
nwww.a = InputBox("输入nwww.a", nwww.a)
nwww.b = InputBox("输入nwww.b", nwww.b)
nwww.c = InputBox("输入nwww.c", nwww.c)
Open "c:\s.dat" For Random As #1 Len = 12
recordnum = LOF(1) \ Len(nwww)
Text5 = recordnum
recordnum = recordnum + 1
Put #1, recordnum, nwww
Close #1
End Sub
Function strHexToNum(s As String) As Integer
dim v as integer
v = CInt("&H" & left$(s,1))
if len(s)>1 then
v = v + CInt("&H" & Mid$(s,2,1)) * &H100
end if
strHexToNum = v
End Function
SubTable_Size = Payload_Data_Length * 256
竟然溢出,无语呀
另外,我想问下,我在结构如何定义3个/5个bit的变量,
我用文件binary操作的PUT#1 ,,变量,这个变量时
如:V是一个byte数据,这三个bit的V写进文件有没有问题呀!我就怕数据不对
我给贴子加分去
type aaa
A as byte '这里只能为5个bit,但VB没有Bit
B as long '25个bit
c as byte '1个bit
d as integer '因为是8个bit,我定义成了整型
f as byte '3 bit
end type咋样才能将这个结构中的值连在一个Var的变量中,并且这个结构的长度我咋得出呀下边说明一下:我现在就卡在这了,其实上边的也是这样的,我要得出这个结构CRC32的值(这个我可以实现了),现在就是想求结构的长度(字节算),把这个结构中的所有变量连接在一起,我好计算CRC值(最重要的)
type aaa
A '5个bit
B '15个bit
c '1个bit
d '8个bit
f '3 bit
end type
ID As Integer
Name As String * 20
End TypeDim MyRecord As Record, RecordNumber ' 声明变量。
' 以随机访问方式打开文件。
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
For RecordNumber = 1 To 5 ' 循环五次。
MyRecord.ID = RecordNumber ' 定义 ID。
MyRecord.Name = "My Name" & RecordNumber ' 建立字符串。
Put #1, RecordNumber, MyRecord ' 将记录写入文件中。
Next RecordNumber
Close #1 ' 关闭文件。
Private Type TS_Software_Descriptor
Descriptor_Tag As Byte
Descriptor_Length As Byte
Software_Version As String * 16
Software_Size As String * 32
Software_CRC As String * 32
Last_Table_Id As String * 5
Reserved_Feature_Use3 As String * 3
End Type
Dim TS_Descriptor As TS_Software_Descriptor
Private Type TS_Data_Section
Table_Id As Byte
Section_Syntax_Indicator As String * 1 '假设长度为1
Reserved_Feature_Use3 As String * 3
Section_Length As String * 12
Reserved_Feature_Use13 As String * 13
Sub_Table_Id As String * 5
Reserved_Feature_Use2 As String * 2
Version_Number As String * 5
Current_Next_Indicator As String * 1 '假设长度为1
Section_Number As Byte
Last_Section_Number As Byte
Reserved_Feature_Use4 As String * 4
Descriptor_Loop_Length As String * 12
TS_Descriptor(1 To 90) As Byte
Payload_Data(0 To 3072) As Byte
CRC32 As String * 32
End Type
Dim ab As TS_Data_Section
Private Sub Command1_Click()
Print UBound(ab.Payload_Data)
Print Len(ab.Section_Syntax_Indicator)
Print Len(TS_Descriptor)
Print Len(ab)
End Sub
http://topic.csdn.net/u/20080108/14/67783c1e-1a7e-4613-904c-dda5e08a380b.html
Option ExplicitPrivate Data(0 To 5) As Byte'[0]: 0-4
Public Property Get A() As Byte
A = Data(0) And &H1F
End PropertyPublic Property Let A(ByVal RHS As Byte)
Data(0) = (Data(0) And &HE0) Or (RHS And &H1F)
End Property'[0]: 5-7, [1]:0-7, [2]:0-7, [3]:0-5
Public Property Get B() As Long
B = (Data(0) \ &H20) Or _
(Data(1) * 8) Or _
(Data(2) * &H800&) Or _
(Data(3) And &H3F) * &H80000
End PropertyPublic Property Let B(ByVal RHS As Long)
Data(0) = (Data(0) And &H1F) Or ((RHS And 7) * &H20)
Data(1) = (RHS And &H7F8&) \ 8
Data(2) = (RHS And &H7F800) \ &H800&
Data(3) = (Data(3) And &HC0) Or ((RHS And &H1F80000) \ &H80000)
End Property'[3]:6
Public Property Get C() As Byte
C = (Data(3) And &H40) \ &H40
End PropertyPublic Property Let C(ByVal RHS As Byte)
Data(3) = (Data(3) And &HBF) Or ((RHS And 1) * &H40)
End Property'[3]:7, [4]:0-6
Public Property Get D() As Byte
D = (Data(3) \ &H80) Or _
((Data(4) And &H7F) * 2)
End PropertyPublic Property Let D(ByVal RHS As Byte)
Data(3) = (Data(3) And &H7F) Or ((RHS And 1) * &H80)
Data(4) = (Data(4) And &H80) Or (RHS \ 2)
End Property'[4]:7, [5]:0-1
Public Property Get F() As Byte
F = (Data(4) \ &H80) Or _
((Data(5) And 3) * 2)
End PropertyPublic Property Let F(ByVal RHS As Byte)
Data(4) = (Data(4) And &H7F) Or ((RHS And 1) * &H80)
Data(5) = (RHS And 6) \ 2
End Property