使用.NET的二进制转换器到十进制

时间:2014-01-21 06:33:43

标签: vb.net binary converter

我开始掌握一些编程基础,但我仍然很新,没有经验。我正在编写一个我正在编码的新程序。

我希望有一个程序,其中8位二进制数放在文本框中,按下按钮,并显示二进制数的十进制值。

以下是我尝试的代码:

Public Class Form1

Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click

    Dim N As Integer
    N = Convert.ToDouble(tbxBin.Text)
    N = N Mod 2
    N = N \ 2
    lblAns.Text = Convert.ToString(N)

End Sub
End Class

可悲的是,当我运行程序时,无论我输入什么二进制代码,我都只得到十进制数。我很困惑,因为我被告知使用Mod类。当我在网上寻找解决问题的方法时,我会看到我以前从未见过的技术和各种编码过程。

显然,对于我的代码,我必须重复我输入的内容,因为用户输入了很多位,但是,我很困惑我是如何去做的。我是否对新变量进行了暗淡处理?

非常感谢任何和所有帮助。谢谢。

5 个答案:

答案 0 :(得分:2)

编辑:请参阅下面的其他方式!!我误读了。

二进制到十进制:

Public Function ConvertFromBinary(ByVal input As String) As Integer
    Dim ret As Integer = 0
    Dim splitInput As Char() = input.ToCharArray
    Dim modifier As Integer = 1
    For i As Integer = splitInput.Length - 1 To 0 Step -1
        Dim thisChar As Integer = Val(splitInput(i))
        If thisChar = 1 Then
            ret += thisChar * modifier
        End If
        modifier *= 2
    Next

    Return ret
End Function

......又回来了:

简单回答?

Convert.ToString(input, 2) 

然而,这并不好玩:

Public Function ConvertToBinary(ByVal input As Integer) As String

    Dim ret As String = ""

    While input > 0
        ret &= input Mod 2
        input = input \ 2
    End While

    Return StrReverse(ret)
End Function

答案 1 :(得分:1)

艰难的方式

Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click
    Dim pwrOf2 As Integer = 0
    Dim ans As Long = 0L
    'first check if the user input a number
    If Long.TryParse(tbxBin.Text, Nothing) Then
        'then look at each char in reverse
        For Each n As Char In tbxBin.Text.Reverse
            Select Case n
                Case Is = "0"c
                Case Is = "1"c
                    ans += 1L << pwrOf2
                Case Else
                    'error - input not in binary, only 1's and 0's 
                    Stop
            End Select
            pwrOf2 += 1
        Next
        lblAns.Text = ans.ToString("n0")
    Else
        'not a number
    End If
End Sub

答案 2 :(得分:1)

只是一个建议,首先要做一个像这样的函数:

    Public Function Bin_To_Dec(ByVal Bin As String)
        Dim dec As Double = Nothing
        Dim length As Integer = Len(Bin)
        Dim temp As Integer = Nothing
        Dim x As Integer = Nothing

        For x = 1 To length
            temp = Val(Mid(Bin, length, 1))
            length = length - 1
            If temp <> "0" Then
                dec += (2 ^ (x - 1))
            End If
        Next

        Return dec
    End Function
然后用它来调用它:

txtZ.Text = Bin_To_Dec(txtX.Text)

答案 3 :(得分:0)

首先转换为整数数组,然后将其转换为二进制,请参阅下面的代码

        public static Byte[] SerializeDecimal(Decimal decimalValue)
    {
        Int32[] arraydDecimalToInt32 = Decimal.GetBits(decimalValue); ;
        Byte[] byteData = new byte[sizeof(Int32) * arraydDecimalToInt32.Length];

        for (Int32 index2 = 0; index2 < arraydDecimalToInt32.Length; index2++)
        {
            Array.Copy(BitConverter.GetBytes(arraydDecimalToInt32[index2]), 0, byteData, index2 * sizeof(Int32), sizeof(Int32));
        }
        return byteData;
    }
    public static Decimal DeserializeDecimal(  Byte[] data)
    {
        Int32[] parts = new Int32[4];
        for (Int32 index = 0; index < 4; index++)
        {
            parts[index] = BitConverter.ToInt32(data, index*4);
        }
        bool sign = (parts[3] & 0x80000000) != 0;
        byte scale = (byte)((parts[3] >> 16) & 0x7F);
        return new Decimal(parts[0], parts[1], parts[2], sign, scale);
        //See http://msdn.microsoft.com/en-us/library/system.decimal.getbits(v=vs.110).aspx
    }

答案 4 :(得分:-1)

Sub Main()

    Dim binarystring As String
    Dim bitvalue As Integer
    Dim denaryvalue As Integer = 0
    Dim bit As String
    Dim i As Integer
    Console.WriteLine("Enter the binary string")
    binarystring = Console.ReadLine
    Dim lenstring As Integer = Len(binarystring)
    For i = 1 To Len(binarystring)
        bit = Mid(binarystring, i, 1)
        bitvalue = Int(bit)
        denaryvalue = 2 ^ (lenstring - i) * bitvalue + denaryvalue
    Next
    Console.WriteLine(denaryvalue)
    Console.ReadLine()

End Sub