# Re: 请教一下

Posted by Honey on November 12, 1998 at 23:27:31:

In Reply to: 请教一下 posted by Steven on November 12, 1998 at 13:56:06:

提供以下程式给你
不过侦错的部分得靠你自己写
例如在2进位输入非0及1的数.....

在Form上摆 Command1 Text1 Text2 Text3 Text4
Text1用以储存要转换的数值
Text2用以储存要转换数值的位元数(2,8,10,16位元)
Text4用以储存'转换后数值的'位元数''(2,8,10,16位元)
Text3是结果

Option Explicit

Private Sub Command1_Click()
If Text4.Text = 2 Then
Text3.Text = ch2bit(Text2.Text, _
Text1.Text)
ElseIf Text4.Text = 8 Then
Text3.Text = ch8bit(Text2.Text, _
Text1.Text)
ElseIf Text4.Text = 10 Then
Text3.Text = ch10bit(Text2.Text, _
Text1.Text)
ElseIf Text4.Text = 16 Then
Text3.Text = ch16bit(Text2.Text, _
Text1.Text)
Else
End If
End Sub

Public Function ch16bit(BIT As Byte, _
s As String) As String
Dim k As Long, i As Long, h As Long
Dim d
k = Len(Text1.Text)
For i = 1 To k
d = Mid(s, i, 1)
h = h + BIT ^ (k - i) * CByte(d)
Next
ch16bit = Hex(h)
End Function

Public Function ch8bit(BIT As Byte, _
s As String) As String
Dim k As Long, i As Long, h As Long
Dim d
k = Len(Text1.Text)
For i = 1 To k
d = Mid(s, i, 1)
h = h + BIT ^ (k - i) * CByte(d)
Next
ch8bit = Oct(h)
End Function

Public Function ch2bit(BIT As Byte, _
s As String) As String
Dim k As Long, i As Long, h As Long
Dim d, u
k = Len(Text1.Text)
For i = 1 To k
d = Mid(s, i, 1)
h = h + BIT ^ (k - i) * CByte(d)
Next
i = 0
Do While 2 ^ i <= h
u = 2 ^ i
If u And h Then
ch2bit = "1" & ch2bit
Else
ch2bit = "0" & ch2bit
End If
i = i + 1
Loop
End Function

Public Function ch10bit(BIT As Byte, _
s As String) As String
Dim k As Long, i As Long, h As Long
Dim d, u
k = Len(Text1.Text)
For i = 1 To k
d = Mid(s, i, 1)
h = h + BIT ^ (k - i) * CByte(d)
Next
ch10bit = CStr(h)
End Function

Follow Ups: