重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
10进制数转换为2进制数的方法,其实就是把10进制数不断除以2,然后把每次相除的余数按逆序的方式排列出来就是了(此方法适用于10进制数转换为任意n进制数,只要把上述的2换为n即可,当然,如果n大于10,就要用其他符号来代替大于10的数了,比如16进制的ABCDEF)
创新互联为企业级客户提高一站式互联网+设计服务,主要包括成都网站建设、网站制作、重庆APP软件开发、微信平台小程序开发、宣传片制作、LOGO设计等,帮助客户快速提升营销能力和企业形象,创新互联各部门都有经验丰富的经验,可以确保每一个作品的质量和创作周期,同时每年都有很多新员工加入,为我们带来大量新的创意。
d = Val(InputBox("请任意输入一个整数")) 'd是任意一个10进制整数
n = 2 '转为二进制
h = "" 'h是用来存放二进制数的字符串
Do While d 0 '如果d大于0就继续循环
h = (d Mod 2) h '把d与2相除的余数进行拼接
d = d \ 2 '把d与2进行整除
Loop
MsgBox d "转为二进制是" h '显示结果
VB.NET控制台程序:
Module module3
Sub Main()
Dim i As Integer
Dim num As Integer = -35
Dim R As Integer = 8
Dim arr(33) As Integer
Dim len As Integer
arr = TransToR(num, R)
len = arr(0)
Console.Write(num " 转换成 " R " 进制后:" vbTab)
If (arr(1) = 1) Then
Console.Write("-")
End If
For i = len + 1 To 2 Step -1
Console.Write(arr(i))
Next
Console.WriteLine()
arr = TransToSourceCode(num)
Console.Write(num " 转换成32位二进制源码后:" vbTab)
Console.Write(arr(1))
For i = 32 To 2 Step -1
Console.Write(arr(i))
Next
Console.WriteLine()
arr = TransToReverseCode(num)
Console.Write(num " 转换成32位二进制反码后:" vbTab)
Console.Write(arr(1))
For i = 32 To 2 Step -1
Console.Write(arr(i))
Next
Console.WriteLine()
arr = TransToCompensatoryCode(num)
Console.Write(num " 转换成32位二进制补码后:" vbTab)
Console.Write(arr(1))
For i = 32 To 2 Step -1
Console.Write(arr(i))
Next
Console.WriteLine()
End Sub
'将十进制数num转换为R(2=R=9)进制数,结果存在arr数组中
'arr(0)存储长度, arr(1)存储符号位:1为负,0为正
Function TransToR(ByVal num As Integer, ByVal R As Integer) As Integer()
Dim arr(33) As Integer
arr(0) = 0 '初始长度
'设置符号位
If (num 0) Then
arr(1) = 1
Else
arr(1) = 0
End If
num = Math.Abs(num)
While num 0
arr(0) = arr(0) + 1
arr(arr(0) + 1) = num Mod R
num \= R
End While
Return arr
End Function
'将十进制数num转换为二进制原码
Function TransToSourceCode(ByVal num As Integer) As Integer()
Dim arr(33) As Integer
Dim i As Integer
arr = TransToR(num, 2)
'高位补零
For i = arr(0) + 2 To 32
arr(i) = 0
Next
arr(0) = 32
Return arr
End Function
'将十进制数num转换为二进制反码
Function TransToReverseCode(ByVal num As Integer) As Integer()
Dim arr(33) As Integer
Dim i As Integer
arr = TransToSourceCode(num)
If num = 0 Then
Return arr
End If
'除符号位外各位取反
For i = 2 To 33
arr(i) = 1 - arr(i)
Next
Return arr
End Function
'将十进制数num转换为二进制补码
Function TransToCompensatoryCode(ByVal num As Integer) As Integer()
Dim arr(33) As Integer
Dim i As Integer
Dim c As Integer = 0 '进位
arr = TransToReverseCode(num)
If num = 0 Then
Return arr
End If
'末尾加1
i = 2
arr(i) += 1
Do While True
arr(i) = c + arr(i)
c = arr(i) \ 2
arr(i) = arr(i) Mod 2
i += 1
If c 1 Then
Exit Do
End If
Loop
Return arr
End Function
End Module
1、在VB中没有一种所谓的“真正的二进制”,只有long、integer、byte这样的整数。
2、VB中不支持二进制字符串。只支持16进制字符串,需要在前面加前缀"H"。
比如:CLng("HFFFF")
3、如果你想将"1010011101"这样的二进制字符串转换为数值,需要自己写一个函数。
下面是我给你写的一个:
Function BinaryValue(ByVal strBin As String) As Long
Dim lngOutValue As Long
Dim bytBytes() As Byte
Dim bytAscii(0 To 255) As Byte
Dim lngBytes_Start As Long
Dim lngBytes_Length As Long
Dim lngBytes_Index As Long
bytAscii(49) = 1
bytBytes() = StrConv(strBin, vbFromUnicode)
lngBytes_Length = UBound(bytBytes)
If lngBytes_Length 30 Then lngBytes_Start = lngBytes_Length - 30
For lngBytes_Index = lngBytes_Start To lngBytes_Length
lngOutValue = lngOutValue + bytAscii(bytBytes(lngBytes_Index)) * 2 ^ (lngBytes_Length - lngBytes_Index)
Next
BinaryValue = lngOutValue
End Function
BinaryValue函数可以将31位以下的二进制字符串转换为Long类型整数。
比如下面的用法:
MsgBox (BinaryValue("1111111111111111111111111111110"))
4、如果你想获取一个整数的二进制位可以使用下面的函数:
Function BinaryBit(ByVal lngValue As Long, ByVal lngIndex As Long) As Long
BinaryBit = (lngValue \ 2 ^ lngIndex) Mod 2
End Function
lngValue是long类型数值;
lngIndex是二进制位,从0开始计算,最大值是30。第0位是二进制最右边的那个位。
比如5的二进制是"101"
MsgBox BinaryBit(5, 0) 返回1
MsgBox BinaryBit(5, 1) 返回0
MsgBox BinaryBit(5, 2) 返回1
下面演示如何获取HFF(255)的二进制字符串。
Dim i As Long
Dim s As String
For i = 0 To 30
s = BinaryBit(HFF, i) s
Next
MsgBox s
这样:
Private Sub Command1_Click()
Dim n As Integer, num As Long
Dim ch As String, Bin() As String, r As Integer, i As Integer
num = Val(Text1.Text)
n = 0
r = num
Do
n = n + 1
ReDim Preserve Bin(n)
Bin(n) = r Mod 2
r = r \ 2
Loop Until r 1
For i = UBound(Bin) To 1 Step -1
ch = ch Bin(i)
Next i
Text2.Text = ch
End Sub
redim数组变量没有给其赋值。
扩展资料:
注意事项
一个十进制转二进制的函数:
Public Function D2B(D As Integer) As String
D2B = ""
Do While D 0
D2B = D Mod 2 D2B
D = D \ 2
Loop
End Function
Private Sub Form_Load()
Me.Caption = D2B(8)
End Sub
另外的办法:
Function dToTwo(n As Integer) As String
Dim S As String
S = ""
Do While n 0
a = n Mod 2
n = n \ 2
S = Chr(48 + a) S
Loop
dToTwo= S
End Function