0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3200800
  • 21
  • 3
  • 3
  • 5
  • I know this question posted long time ago, but.... will any of provided answers convert **decimal** to binary? I don't think so. Or question is titled wrong, or all provided answers are incorrect. – nelek Aug 30 '15 at 20:52

9 Answers9

1

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

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.ToString method:

output = Convert.ToString(dec, 2)
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
1

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

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
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
0

Heres the VB.NET version for you

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 #

Community
  • 1
  • 1
spajce
  • 7,044
  • 5
  • 29
  • 44
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
PeterCo
  • 910
  • 2
  • 20
  • 36
Kaz-LA
  • 226
  • 1
  • 5
  • Your VB version does not work correctly due to the line `num /= 2` because the invisible R.H.S. is a double and the implicit conversion to an integer uses rounding, not truncation. E.g. with 1399775318 your method gives 1010100100100010010000010101010 but the correct answer is 1010011011011101110000001010110. To correct it, use `num = num \ 2` (or `num \= 2`). – Andrew Morton Jan 18 '17 at 11:14
0

Take this code to your btnCalc and paste it

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

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
0

So my solution with ULong and Option Strict On.

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

Compare it with: 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
Thias
  • 1
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"))
Soteris 92
  • 41
  • 3
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
IPSAteck
  • 3
  • 1
0

My solution in VB.Net:

Sub Main(args As String())

    Dim number As Integer = 14
    Dim binary As String

    While number > 0
        Dim temp As String = number Mod 2
        number = Floor(number / 2)  

        binary += temp
    End While

    Console.WriteLine(binary.Reverse.ToArray)

End Sub