English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

我拉了四個物件:text1;command1;command2;command3
【流程】1.讀檔text.txt
2.將text.txt裡的資料\"I Love You.\"轉成ASCI再轉成Binary,輸出至output1.txt
3.將Binary轉成原來的\"I Love You.\"輸出至output2.txt
【問題】1.流程1~2已經有了,但是流程2轉出的Binary彼此間有空格,要如何改成無空格?
2.求流程3的程式
_______________________________________________________
Private Sub Command1_Click() \'讀檔text.txt,把資料顯示到text1
Dim buff1 As String
Open App.Path & \"\\test.txt\" For Input As #1
Do Until EOF(1)
Line Input #1, buff1
Text1.Text = buff1 & vbCrLf
Loop
Close #1
Text1 = Left(Text1, Len(Text1) - 1)
End Sub
______________________________________________________
Private Sub Command2_Click() \'將text.txt資料轉ASCII再轉Binary,輸出至output1.txt
ReDim A(FileLen(App.Path & \"\\test.txt\") - 1) As Byte \'讀檔\"
Open App.Path & \"\\test.txt\" For Binary As #1
Get #1, , A
Close #1

Dim S As String
S = StrConv(A, vbUnicode) \'轉成Unicode
Dim S1 As String, I As Integer
For I = 1 To Len(S) \'轉二進位
S1 = S1 & TTo2(Asc(Mid$(S, I, 1))) & \" \"
Next

Open \"C:\\output1.txt\" For Output As #1 \'輸出至\"C:\\output1.txt\"
Print #1, Left$(S1, Len(S1) - 1)
Close #1
End Sub
___________________________________________________
Function TTo2(ByVal N As Integer) As String \'十進位轉二進位
Do
TTo2 = N Mod 2 & TTo2: N = N \\ 2
Loop Until N = 0
End Function
____________________________________________________
Private Sub Command3_Click() \'將Binary再轉回原資料,輸出至output2.txt

???

End Sub
_____________________________________________________
麻煩了 謝謝喔!

2006-03-23 18:15:58 · 1 個解答 · 發問者 阿雷好了 2 in 電腦與網際網路 程式設計

1 個解答

'之前我將A()轉成字串再轉ASCII是多餘的,A()裡面的資料本來就是ASCII碼,現在就直接將A()轉二進位'每個值用" "分隔是方便讀取,若不使用就須以固定長度的方式來寫入(長度=8,Ex:255=11111111)'這次方式的好處是中文2個byte,英文1個byte,每個byte不會超過255,之前的方式若遇到中文(-2564...)我寫的十進位轉二進位副函數就必須修改了,且固定長度就必須增加,造成檔案增肥.Private Sub Command2_Click()ReDim A(FileLen("C:\test.txt") - 1) As ByteOpen "C:\test.txt" For Binary As #1Get #1, , AClose #1Dim S1 As String, I As IntegerFor I = 0 To UBound(A) '轉二進位    S1 = S1 & Format(TTo2(A(I)), String(8, "0")) '長度不足8前面補0NextOpen "C:\output1.txt" For Output As #1 '輸出Print #1, S1Close #1End SubPrivate Sub Command3_Click()Dim A() As Byte, X&, I&Open "C:\output1.txt" For Input As #1For I = 1 To LOF(1) - 8 Step 8    Seek #1, I    ReDim Preserve A(X)    A(X) = tToT(Input(8, #1)): X = X + 1 '每8個字讀取1次再轉成十進位放到A()NextClose #1Open "C:\output2.txt" For Binary As #1 '輸出Put #1, , AClose #1End SubFunction tToT(ByVal N As String) As Integer '二進位轉十進位Dim I%For I = 1 To Len(N)    If Mid$(N, I, 1) = 1 Then tToT = tToT + (2 ^ (Len(N) - I))Next IEnd Function

2006-03-24 10:10:35 · answer #1 · answered by W.J.S. 7 · 0 0

fedest.com, questions and answers