Click to See Complete Forum and Search --> : Text to Binary file conversion


zenn0
October 2nd, 2001, 03:52 AM
Hello

Is there any utilities/activeX/source code which I can use to convert an ascii text file into a binary file????
Thanks!

Iouri
October 2nd, 2001, 07:17 AM
Private Sub cmdText_Click() 'This sub converts the text to binary
Dim lInput As Double
Dim sOutput As String
Dim sOutput2 As String
Dim iX, iY As Integer

If Len(txtText) > 0 Then 'if valid text exists
sOutput2 = "" 'reset output string
For iY = 1 To Len(txtText.Text) 'for each character in the text
lInput = Asc(Mid(txtText.Text, iY, 1)) 'retreive the Ascii value of this character
sOutput = "" 'reset the output string for this character
While lInput > 0
iX = lInput Mod 2 'return the modulus
sOutput = iX & sOutput 'append result to output string for this character
lInput = Int(lInput / 2) 'return the next number for calculation
Wend
While Len(sOutput) < 8
sOutput = 0 & sOutput 'add leading zeros if the result is shorter than 8 characters
Wend
sOutput2 = sOutput2 & " " & sOutput 'append result and a separating space to output string
Next
MsgBox "The binary eqivalent of the text string '" & txtText.Text & "' is:" & vbLf & vbLf & sOutput2, vbInformation + vbOKOnly, "Success" 'display result
txtText.Text = "" 'reset text box
Else 'if no valid text exists
MsgBox "Please enter some text.", vbCritical + vbOKOnly, "Error"
End If
txtText.SetFocus 'reset focus to textbox
End Sub

Private Sub txtNumber_Change()
Dim X As Integer

If IsNumeric(txtNumber.Text) = False Then 'if latest character isn't a number
If Len(txtNumber.Text) > 1 Then
txtNumber.Text = Left(txtNumber.Text, Len(txtNumber.Text) - 1) 'remove the last caracter from the string
Else
txtNumber.Text = ""
End If
Else
If InStr(1, txtNumber.Text, ".", vbTextCompare) > 0 Then 'remove any periods
txtNumber.Text = Left(txtNumber.Text, Len(txtNumber.Text) - 1)
ElseIf InStr(1, txtNumber.Text, "+", vbTextCompare) > 0 Then 'remove any plus signs
txtNumber.Text = Left(txtNumber.Text, Len(txtNumber.Text) - 1)
ElseIf InStr(1, txtNumber.Text, "-", vbTextCompare) > 0 Then 'remove any minus signs
txtNumber.Text = Left(txtNumber.Text, Len(txtNumber.Text) - 1)
ElseIf InStr(1, txtNumber.Text, "$", vbTextCompare) > 0 Then 'remove any dollar signs
txtNumber.Text = Left(txtNumber.Text, Len(txtNumber.Text) - 1)
ElseIf InStr(1, txtNumber.Text, ",", vbTextCompare) > 0 Then 'remove any commas
txtNumber.Text = Left(txtNumber.Text, Len(txtNumber.Text) - 1)
End If
End If
txtNumber.SelStart = Len(txtNumber.Text) 'reset the cursor position
End Sub


Iouri Boutchkine
iouri@hotsheet.com