VB中怎么随机生成简体汉字
展开全部
''下面是函数
''-------------------------------------------
Public Function dec2bin(mynum) ''十进制到二进制
Dim loopcounter
If mynum >= 2 ^ 31 Then
dec2bin = "Too big"
Exit Function
End If
Do
If (mynum And 2 ^ loopcounter) = 2 ^ loopcounter Then
dec2bin = "1" & dec2bin
Else
dec2bin = "0" & dec2bin
End If
loopcounter = loopcounter + 1
Loop Until 2 ^ loopcounter > mynum
End Function
''-------------------------------------------
Public Function BinaryToDecimal(BinaryValue) ''二进制到十进制的转换
'' Returns the decimal equivalent of a binary number
Dim idx
Dim tmp
Dim result
Dim digits
digits = Len(BinaryValue)
For idx = digits To 1 Step -1
tmp = Mid(BinaryValue, idx, 1)
If tmp = "1" Then result = result + 2 ^ (digits - idx)
Next
BinaryToDecimal = result
End Function
Private Sub Command1_Click()
Dim i
Dim j
Dim HS
Dim HE
Dim LS
Dim LE
Dim result
result = ""
HS = 177
HE = 247
LS = 161
LE = 254
Dim Max_Num
Max_Num = 65536
Randomize
For i = 1 To 4
temp1 = dec2bin(Int((HE - HS) * Rnd()) + HS)
temp2 = dec2bin(Int((LE - LS) * Rnd()) + LS)
result = result & Chr(BinaryToDecimal(temp1 & temp2) - Max_Num)
Next
Me.Print result & " "
End Sub
''-------------------------------------------
Public Function dec2bin(mynum) ''十进制到二进制
Dim loopcounter
If mynum >= 2 ^ 31 Then
dec2bin = "Too big"
Exit Function
End If
Do
If (mynum And 2 ^ loopcounter) = 2 ^ loopcounter Then
dec2bin = "1" & dec2bin
Else
dec2bin = "0" & dec2bin
End If
loopcounter = loopcounter + 1
Loop Until 2 ^ loopcounter > mynum
End Function
''-------------------------------------------
Public Function BinaryToDecimal(BinaryValue) ''二进制到十进制的转换
'' Returns the decimal equivalent of a binary number
Dim idx
Dim tmp
Dim result
Dim digits
digits = Len(BinaryValue)
For idx = digits To 1 Step -1
tmp = Mid(BinaryValue, idx, 1)
If tmp = "1" Then result = result + 2 ^ (digits - idx)
Next
BinaryToDecimal = result
End Function
Private Sub Command1_Click()
Dim i
Dim j
Dim HS
Dim HE
Dim LS
Dim LE
Dim result
result = ""
HS = 177
HE = 247
LS = 161
LE = 254
Dim Max_Num
Max_Num = 65536
Randomize
For i = 1 To 4
temp1 = dec2bin(Int((HE - HS) * Rnd()) + HS)
temp2 = dec2bin(Int((LE - LS) * Rnd()) + LS)
result = result & Chr(BinaryToDecimal(temp1 & temp2) - Max_Num)
Next
Me.Print result & " "
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询