关键问题是,你的 Ttext 中是什么东西,要做何种转换。 下例假定是字符,将其 ASCII 码写入文件:Dim arr() As Bytearr = StrConv(Text1, vbFromUnicode) '如果要求 ANSI 格式 Open "c:\my documents\test.bin" For Binary As #1 Put #1, , arr Close #1
Option ExplicitPrivate Sub Command1_Click() Dim str_Temp As String Dim str_Value As String Dim i As Integer Dim j As Integer Dim EFile As Integer Dim EFilePath As StringIf Not IsNull(Text1.Text) Then If CInt(Text1.Text) < 10000 Or CInt(Text1.Text) > 0 Then ' 加一个数据范围判断,根据你的需要加 str_Temp = CStr(Hex(CInt(Text1.Text))) ' cint 也可以用 int 中间有四舍五入的差别 i = Len(str_Temp) For j = 1 To i Select Case Mid(str_Temp, j, 1) Case "0" str_Value = str_Value & "0000" Case "1" str_Value = str_Value & "0001" Case "2" str_Value = str_Value & "0010" Case "3" str_Value = str_Value & "0011" Case "4" str_Value = str_Value & "0100" Case "5" str_Value = str_Value & "0101" Case "6" str_Value = str_Value & "0110" Case "7" str_Value = str_Value & "0111" Case "8" str_Value = str_Value & "1000" Case "9" str_Value = str_Value & "1001" Case "A" str_Value = str_Value & "1010" Case "B" str_Value = str_Value & "1011" Case "C" str_Value = str_Value & "1100" Case "D" str_Value = str_Value & "1101" Case "E" str_Value = str_Value & "1110" Case "F" str_Value = str_Value & "1111" End Select Next j ' 如果需要将头部分的0去除,对str_Value进行字符串操作,不累叙了
' 以下保存到文件,使用追加的办法 EFile = FreeFile EFilePath = App.Path & "\a.bin" ' 你的BIN文件放置位置 Open EFilePath For Append As #EFile Print #EFile, str_Value Close #EFile End IfEnd IfEnd Sub呵呵,随便想了一个,有更好的办法大家推荐,呵呵
楼主是想做 string --> byte 的转换吧?Dim arrbyte() As Byte arrbyte = StrConv(Text1.text, vbFromUnicode)arrbyte 即是二进制流,用 put 方法写入即可。
先打开文件,再移动指针open "a.txt" for binary as #1 Seek #1, LOF(1) + 1 Put #1, , x 'x为要写入的byte() Close #1
我试着写了一个可是不行啊。写的文件用记事本打开还是能看见这是为什么啊? 代码如下:Const ARR_NUM As Integer = 1024 Dim arr(0 To ARR_NUM - 1) As Byte Dim arr1() As Byte Dim filelen, i, k As Integer Dim readfilenum As Integer k = 1 filelen = Len(RichTextBox1.Text) readfilenum = FreeFile() Open App.Path + "\log.dat" For Binary As readfilenum i = 0 For k = 0 To filelen
arr(i) = CByte(Asc(Mid(RichTextBox1.Text, k, 1))) If i >= ARR_NUM - 1 Then Put #readfilenum, , arr i = 0 Else i = i + 1 End If Next Close #readfilenum MsgBox "文件保存成功!"这是为什么啊!! 谁能帮我看看
Const ARR_NUM As Integer = 2048 Dim arr() As Byte ReDim arr(0 To ARR_NUM - 1) As Byte Dim a As Byte Dim arr1() As Byte Dim filelen, i, k As Integer Dim readfilenum As Integer k = 1 filelen = Len(RichTextBox1.Text) readfilenum = FreeFile() Open App.Path + "\log.dat" For Binary As readfilenum Seek #readfilenum, LOF(readfilenum) + 2
'If filelen > ARR_NUM * k Then ' For i = 0 To ARR_NUM ' arr(i) = AscB(Mid(RichTextBox1.Text, i + 1, 1)) ' Next 'Else ' For i = 0 To (filelen - ARR_NUM * (k - 1)) ' arr(i) = AscB(Mid(RichTextBox1.Text, i + 1, 1)) ' Next 'Put #readfilenum, , arr 'End If 'arr1 = StrConv(RichTextBox1.Text, vbFromUnicode) i = 0 For k = 1 To filelen
arr(i) = CByte(Asc(Mid(RichTextBox1.Text, k, 1))) 'arr(i) = arr1(k) If i >= ARR_NUM - 1 Then Put #readfilenum, , arr 'Debug.Print arr i = 0 ReDim arr(0 To ARR_NUM - 1) As Byte Else i = i + 1 End If Next Put #readfilenum, , arr Close #readfilenum MsgBox "文件保存成功!"
下例假定是字符,将其 ASCII 码写入文件:Dim arr() As Bytearr = StrConv(Text1, vbFromUnicode) '如果要求 ANSI 格式
Open "c:\my documents\test.bin" For Binary As #1
Put #1, , arr
Close #1
Dim str_Temp As String
Dim str_Value As String
Dim i As Integer
Dim j As Integer
Dim EFile As Integer
Dim EFilePath As StringIf Not IsNull(Text1.Text) Then
If CInt(Text1.Text) < 10000 Or CInt(Text1.Text) > 0 Then ' 加一个数据范围判断,根据你的需要加
str_Temp = CStr(Hex(CInt(Text1.Text))) ' cint 也可以用 int 中间有四舍五入的差别
i = Len(str_Temp)
For j = 1 To i
Select Case Mid(str_Temp, j, 1)
Case "0"
str_Value = str_Value & "0000"
Case "1"
str_Value = str_Value & "0001"
Case "2"
str_Value = str_Value & "0010"
Case "3"
str_Value = str_Value & "0011"
Case "4"
str_Value = str_Value & "0100"
Case "5"
str_Value = str_Value & "0101"
Case "6"
str_Value = str_Value & "0110"
Case "7"
str_Value = str_Value & "0111"
Case "8"
str_Value = str_Value & "1000"
Case "9"
str_Value = str_Value & "1001"
Case "A"
str_Value = str_Value & "1010"
Case "B"
str_Value = str_Value & "1011"
Case "C"
str_Value = str_Value & "1100"
Case "D"
str_Value = str_Value & "1101"
Case "E"
str_Value = str_Value & "1110"
Case "F"
str_Value = str_Value & "1111"
End Select
Next j
' 如果需要将头部分的0去除,对str_Value进行字符串操作,不累叙了
' 以下保存到文件,使用追加的办法
EFile = FreeFile
EFilePath = App.Path & "\a.bin" ' 你的BIN文件放置位置
Open EFilePath For Append As #EFile
Print #EFile, str_Value
Close #EFile
End IfEnd IfEnd Sub呵呵,随便想了一个,有更好的办法大家推荐,呵呵
arrbyte = StrConv(Text1.text, vbFromUnicode)arrbyte 即是二进制流,用 put 方法写入即可。
Seek #1, LOF(1) + 1
Put #1, , x 'x为要写入的byte()
Close #1
代码如下:Const ARR_NUM As Integer = 1024
Dim arr(0 To ARR_NUM - 1) As Byte
Dim arr1() As Byte
Dim filelen, i, k As Integer
Dim readfilenum As Integer
k = 1
filelen = Len(RichTextBox1.Text)
readfilenum = FreeFile()
Open App.Path + "\log.dat" For Binary As readfilenum
i = 0
For k = 0 To filelen
arr(i) = CByte(Asc(Mid(RichTextBox1.Text, k, 1)))
If i >= ARR_NUM - 1 Then
Put #readfilenum, , arr
i = 0
Else
i = i + 1
End If
Next
Close #readfilenum
MsgBox "文件保存成功!"这是为什么啊!!
谁能帮我看看
Dim arr() As Byte
ReDim arr(0 To ARR_NUM - 1) As Byte
Dim a As Byte
Dim arr1() As Byte
Dim filelen, i, k As Integer
Dim readfilenum As Integer
k = 1
filelen = Len(RichTextBox1.Text)
readfilenum = FreeFile()
Open App.Path + "\log.dat" For Binary As readfilenum
Seek #readfilenum, LOF(readfilenum) + 2
'If filelen > ARR_NUM * k Then
' For i = 0 To ARR_NUM
' arr(i) = AscB(Mid(RichTextBox1.Text, i + 1, 1))
' Next
'Else
' For i = 0 To (filelen - ARR_NUM * (k - 1))
' arr(i) = AscB(Mid(RichTextBox1.Text, i + 1, 1))
' Next
'Put #readfilenum, , arr
'End If
'arr1 = StrConv(RichTextBox1.Text, vbFromUnicode)
i = 0
For k = 1 To filelen
arr(i) = CByte(Asc(Mid(RichTextBox1.Text, k, 1)))
'arr(i) = arr1(k)
If i >= ARR_NUM - 1 Then
Put #readfilenum, , arr
'Debug.Print arr
i = 0
ReDim arr(0 To ARR_NUM - 1) As Byte
Else
i = i + 1
End If
Next
Put #readfilenum, , arr
Close #readfilenum
MsgBox "文件保存成功!"
http://www.codesky.net/showcode.asp?uid=36531