satriano17
Gebruiker
- Lid geworden
- 30 sep 2012
- Berichten
- 331
dag
Met deze code kan ik een string, woord converteren naar Binary
Werkt bijna goed. Ik heb namelijk een paar problemen
De eerste probleem is dat conversie naar binary wordt zonder spaces weergegeven
en ik zou willen dat le letters (binary) gescheiden worden weergegeven.
Bv.
Als ik abc naar binary converteer dan wordt in de output
011000010110001001100011
ik zou willen dat de output zo wordt weergegeven
01100001 01100010 01100011
Als ik dan
a b c
invoeg met spaces tussen de letters dan worden ook de spaces geconverteerde En dat moet ook niet
011000010010000001100010001000000110001100100000
Om alles te verduidelijk
als de input
jan wit
is dan moet zo de uitkomst zijn:
01101010 01100001 01101110 01110111 01101001 01110100
dank u
satriano
Met deze code kan ik een string, woord converteren naar Binary
Code:
Private Sub cmdAsciiToBinary_Click()
Dim txt As String
Dim result As String
Dim ch As String
Dim bin As String
Dim i As Integer
txt = txtascii.Text 'input tekst
txt = Replace(txt, vbCr, "")
txt = Replace(txt, vbLf, "")
result = ""
For i = 1 To Len(txt)
ch = Mid$(txt, i, 1)
bin = LongToBinary(Asc(ch), False)
result = result & Right$(bin, 8)
Next i
txtbinary.Text = result ' output binary
End Sub
' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
' LONG TO BINARY
' Convert this Long value into a binary string.
' See also
' http://www.vb-helper.com/howto_net_dec_hex_oct_bin.html.
' This version of LongToBinary does not add a "&B" in the
' front.
Private Function LongToBinary(ByVal long_value As Long, _
Optional ByVal separate_bytes As Boolean = True) As _
String
Dim hex_string As String
Dim digit_num As Integer
Dim digit_value As Integer
Dim nibble_string As String
Dim result_string As String
Dim factor As Integer
Dim bit As Integer
' Convert into hex.
hex_string = Hex$(long_value)
' Zero-pad to a full 8 characters.
hex_string = Right$(String$(8, "0") & hex_string, 8)
' Read the hexadecimal digits
' one at a time from right to left.
For digit_num = 8 To 1 Step -1
' Convert this hexadecimal digit into a
' binary nibble.
digit_value = CLng("&H" & Mid$(hex_string, _
digit_num, 1))
' Convert the value into bits.
factor = 1
nibble_string = ""
For bit = 3 To 0 Step -1
If digit_value And factor Then
nibble_string = "1" & nibble_string
Else
nibble_string = "0" & nibble_string
End If
factor = factor * 2
Next bit
' Add the nibble's string to the left of the
' result string.
result_string = nibble_string & result_string
Next digit_num
If nibble_num Mod 2 <> 0 Then
myfill = ""
Else
myfill = " "
End If
hex_result = myfill & Hex$(nibble_value) & hex_result
' Add spaces between bytes if desired.
If separate_bytes Then
result_string = _
Mid$(result_string, 1, 8) & " " & _
Mid$(result_string, 9, 8) & " " & _
Mid$(result_string, 17, 8) & " " & _
Mid$(result_string, 25, 8)
End If
' Return the result.
LongToBinary = result_string
If nibble_num Mod 2 <> 0 Then
myfill = ""
Else
myfill = " "
End If
hex_result = myfill & Hex$(nibble_value) & hex_result
End Function
De eerste probleem is dat conversie naar binary wordt zonder spaces weergegeven
en ik zou willen dat le letters (binary) gescheiden worden weergegeven.
Bv.
Als ik abc naar binary converteer dan wordt in de output
011000010110001001100011
ik zou willen dat de output zo wordt weergegeven
01100001 01100010 01100011
Als ik dan
a b c
invoeg met spaces tussen de letters dan worden ook de spaces geconverteerde En dat moet ook niet
011000010010000001100010001000000110001100100000
Om alles te verduidelijk
als de input
jan wit
is dan moet zo de uitkomst zijn:
01101010 01100001 01101110 01110111 01101001 01110100
dank u
satriano
Laatst bewerkt: