我对十进制到二进制数转换有点困惑。这是我的代码:
Public Class Form1
Private Sub tbxDecimal_TextChanged(sender As Object, e As EventArgs) Handles tbxDecimal.TextChanged
If rdbDecmial.Checked = True And IsNumeric(tbxDecimal.Text) Then
Dim bin, dec As Double
Dim output As String
dec = Convert.ToDouble(tbxDecimal.Text)
For i = 1 To dec Step (???)
dec = i Mod 2
If i Mod 2 = 0 Then
bin = 0
Else
bin = 1
End If
output &= Convert.ToString(bin)
Next
tbxBinary.Text = output
End If
End Sub
End Class
如果我在一个框中输入十进制数字,则会显示错误的数字。我知道我必须为这个循环设置一些步进大小,但是我应该放入什么?
答案 0 :(得分:1)
您需要While
,而不是For
;只要dec
不是0,就应该循环。另外,您应该将数字视为Integer
而不是Double
;此算法仅适用于整数。另一个问题是你以错误的顺序连接这些位。
Private Function ToBinary(dec As Integer) As String
Dim bin As Integer
Dim output As String
While dec <> 0
If dec Mod 2 = 0 Then
bin = 0
Else
bin = 1
End If
dec = dec \ 2
output = Convert.ToString(bin) & output
End While
If output Is Nothing Then
Return "0"
Else
Return output
End If
End Function
顺便说一句,我猜你是为了学习而手动做的,但如果你不这样做,你可以使用Convert.ToString
方法:
output = Convert.ToString(dec, 2)
答案 1 :(得分:1)
你可以使用二进制字符串表示的内置转换,你也可以使用TryParse来确保文本可以转换为数字(IsNumeric可以比你想要的更宽松一些) :
Private Sub tbxDecimal_TextChanged(sender As Object, e As EventArgs) Handles tbxDecimal.TextChanged
If rdbDecmial.Checked Then
Dim num As Int64
If Int64.TryParse(tbxDecimal.Text, num) Then
tbxBinary.Text = Convert.ToString(num, 2)
Else
tbxBinary.Text = ""
End If
End If
End Sub
答案 2 :(得分:0)
继承了VB.NET版本
Dim decimalNumber As Integer = Integer.Parse(tbxDecimal.Text)
Dim remainder As Integer
Dim result As String = String.Empty
While decimalNumber > 0
remainder = decimalNumber Mod 2
decimalNumber /= 2
result = remainder.ToString() & result
End While
Console.WriteLine("Binary: {0}", result)
答案 3 :(得分:0)
C#:
static string DecimalToBinary(int num)
{
var bin = new System.Text.StringBuilder();
do
{
bin.Insert(0, (num%2));
num/=2;
}while (num != 0);
return bin.ToString();
}
VB:
Private Shared Function DecimalToBinary(num As Integer) As String
Dim bin = New System.Text.StringBuilder()
Do
bin.Insert(0, (num Mod 2))
num /= 2
Loop While num <> 0
Return bin.ToString()
End Function
答案 4 :(得分:0)
将此代码带到您的btnCalc并粘贴
Dim iptbxNumberDec as integer
Dim iptbxNumber as string
Dim intBin as integer
iptbxNumber = inputbox("Please enter the number that you want to convect to binary from decimal","Number ")
integer.tryparse(iptbxNumber,iptbxNumberDec)
if iptbxNumberDec > 0 then
iptbxNumberDec / = 10
Do while iptbxNumberDec <> 0
if iptbxNumberDec mod 2 = 0 then
intBin = 0
lblOutput.text = vbcrlf & vbcrlf & intBin & " "
else
intBin = 1
lblOutput.text = vbcrlf & vbcrlf & intBin & " "
iptbxNumber / = 10
Do while iptbxNumberDec <> 0
if iptbxNumberDec mod 2 = 0 then
intBin = 0
lblOutput.text = vbcrlf & vbcrlf & intBin & " "
else
intBin = 1
lblOutput.text = vbcrlf & vbcrlf & _
intBin & " "
intBin += 1
endif
loop
endif
在visual basic中使用此代码
答案 5 :(得分:0)
所以我的解决方案是ULong和Option Strict On。
例如:num = 4036854942,结果为:11110000100111011000010010011110
将其与:http://unicode-table.com/en/1D11E/
进行比较Private Shared Function DecimalToBinary(num As ULong) As String
Dim bin = New System.Text.StringBuilder()
Dim modnum As Decimal
Do
modnum = num Mod 2
bin.Insert(0, (modnum))
num = CType((num - modnum) / 2, ULong)
Loop While num <> 0
Return bin.ToString()
End Function
答案 6 :(得分:0)
Dim str_bianry as String
Dim index_number as integer =65
while index_number > 0
str &= index_number MOD 2
index_number \=2
end loop
Dim char_binary as char=str.toArray()
Array.Reverse(char_binary)
msgbox(String.join("",char_binary).padLeft(8,"0"))
答案 7 :(得分:0)
VB:
Function DecimalToBinary(num As Integer) As String
Dim Binary_String As String
Binary_String = ""
Do While num > 0
Binary_String = num Mod 2 & Binary_String
num = num \ 2
Loop
Return Binary_String
End Function