如何从十进制转换为二进制 .NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21535919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:37:48  来源:igfitidea点击:

How to convert from decimal to binary .NET

.netvb.net

提问by user3200800

I'm a bit confused on decimal to binary number conversion. Here's my code:

我对十进制到二进制数的转换有点困惑。这是我的代码:

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

If I typed in a decimal number in one box, the incorrect numbers come out. I know I have to have some kind of stepping size for this loop, however what should I put in?

如果我在一个框中输入一个十进制数字,就会出现错误的数字。我知道我必须为这个循环设置某种步进大小,但是我应该输入什么?

回答by Andrew Morton

There is a built-in conversion to a binary string representation which you could use, and you can also use TryParse to make sure the text can be converted to a number (IsNumeric can be a bit more permissive than you might want):

您可以使用内置的二进制字符串表示转换,您还可以使用 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

回答by Thomas Levesque

You need a While, not a For; you should loop as long as decis not 0. Also, you should treat the number as Integerrather than Double; this algorithm only works for integers. Another issue is that you're concatenating the bits in the wrong order.

你需要一个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

BTW, I assume you're doing that manually for learning purposes, but if you're not, you can just use the Convert.ToStringmethod:

顺便说一句,我假设您出于学习目的手动执行此操作,但如果不是,则可以使用该Convert.ToString方法:

output = Convert.ToString(dec, 2)

回答by spajce

Heres the VB.NET version for you

这是给你的 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)

Source: Decimal to binary conversion in c #

来源:c#中的十进制到二进制转换

回答by Kaz-LA

C#:

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:

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

回答by Celumusa

Take this code to your btnCalc and paste it

将此代码带到您的 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  

use this code in visual basic

在 Visual Basic 中使用此代码

回答by Thias

So my solution with ULong and Option Strict On.

所以我使用 ULong 和 Option Strict On 的解决方案。

For example: num = 4036854942 and the result is: 11110000100111011000010010011110

例如:num = 4036854942 结果为:11110000100111011000010010011110

Compare it with: http://unicode-table.com/en/1D11E/

与它进行比较: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

回答by Soteris 92

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"))

回答by IPSAteck

VB:

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