当前位置:编程学习 > VB >>

vb中怎么编写二进制之间的转换???

有谁知道啊??帮帮忙啊????
追问:Public Function DectoBin(dec As Long) As String
DectoBin = ""
Do While dec > 0
DectoBin = dec Mod 2 & DectoBin
dec = dec \ 2
Loop
End Function
Public Function BIN_to_DEC(ByVal Bin As String) As Long
Dim i As Long
For i = 1 To Len(Bin)
BIN_to_DEC = BIN_to_DEC * 2 + Val(Mid(Bin, i, 1))
Next i
End Function
写那个里面啊??????
答案:'16进制转换二进制

Public Function HEX_to_BIN(ByVal Hex As String) As String
Dim i As Long
Dim B As String
Hex = UCase(Hex)
For i = 1 To Len(Hex)
Select Case Mid(Hex, i, 1)
Case "0": B = B & "0000"
Case "1": B = B & "0001"
Case "2": B = B & "0010"
Case "3": B = B & "0011"
Case "4": B = B & "0100"
Case "5": B = B & "0101"
Case "6": B = B & "0110"
Case "7": B = B & "0111"
Case "8": B = B & "1000"
Case "9": B = B & "1001"
Case "A": B = B & "1010"
Case "B": B = B & "1011"
Case "C": B = B & "1100"
Case "D": B = B & "1101"
Case "E": B = B & "1110"
Case "F": B = B & "1111"
End Select
Next i
While Left(B, 1) = "0"
B = Right(B, Len(B) - 1)
Wend
HEX_to_BIN = B
End Function

'10进制转换二进制
Public Function DEC_to_BIN(Dec As Long) As String
DEC_to_BIN = ""
Do While Dec > 0
DEC_to_BIN = Dec Mod 2 & DEC_to_BIN
Dec = Dec \ 2
Loop
End Function

'8进制转换二进制
Public Function OCT_to_BIN(ByVal Oct As String) As String
Dim i As Long
Dim B As String
For i = 1 To Len(Oct)
Select Case Mid(Oct, i, 1)
Case "0": B = B & "000"
Case "1": B = B & "001"
Case "2": B = B & "010"
Case "3": B = B & "011"
Case "4": B = B & "100"
Case "5": B = B & "101"
Case "6": B = B & "110"
Case "7": B = B & "111"
End Select
Next i
While Left(B, 1) = "0"
B = Right(B, Len(B) - 1)
Wend
OCT_to_BIN = B
End Function

新建两个text和两个command
text1为10进制 text2为2进制

把代码全部清空,,在粘贴以下代码

Public Function DectoBin(dec As Long) As String
DectoBin = ""
Do While dec > 0
DectoBin = dec Mod 2 & DectoBin
dec = dec \ 2
Loop
End Function
Public Function BIN_to_DEC(ByVal Bin As String) As Long
Dim i As Long
For i = 1 To Len(Bin)
BIN_to_DEC = BIN_to_DEC * 2 + Val(Mid(Bin, i, 1))
Next i
End Function


Private Sub Command1_Click()
Text2.Text = DectoBin(Int(Text1.Text))
End Sub

Private Sub Command2_Click()
Text1.Text = BIN_to_DEC(Int(Text2.Text))
End Sub

Private Sub Form_Load()
Command1.Caption = "10转2"
Command2.Caption = "2转10"


End Sub

上一个:VB中select case语句怎么用啊!
下一个:VB如何制作数码时钟,给代码

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,