Hi everyone , i am trying to fully uderstand the recursion method in vb but i still have problems in making the programs work, Can anyone help me to create a program that convert decimal numbers to binary numbers or point me to the right direction,I know that this can be done by continuing dividing by 2 until the is no more result left but in a recursive method i have problems. Thank you

Dani AI

Generated

The routine posted in Post #2 implements a mask-based, MSB-first recursion: SubNum is the current power-of-two (start with the largest power <= the number), each call checks whether that power fits into the remaining value, appends a 1 and subtracts it when it does, or appends a 0 and leaves the value when it does not, then halves SubNum and recurses until SubNum reaches 1. One important detail in that code: use AsciiNum - SubNum >= 0 (include equality) so exact power-of-two cases produce a 1; the posted > 0 test will miss exact matches. The routine writes bits directly into txtNew.Text, which produces the final binary string left-to-right (MSB-first).

A simpler, more common recursive pattern uses integer division by 2 and the remainder; it recurses down to the base case and appends remainders on the unwind, which naturally yields MSB-first output. Example implementation in VB.NET:

Function DecimalToBinary(n As Integer) As String
    If n < 2 Then
        Return n.ToString()
    End If
    Return DecimalToBinary(n \ 2) & (n Mod 2).ToString()
End Function

DecimalToBinary(13) returns "1101". The \ operator does integer division in VB.NET; Mod gives the remainder. Returning a string from the function (instead of updating a UI control inside recursion) makes testing and debugging easier.

Practical tips: prefer returning a result and assign it to the UI once (avoid frequent UI updates inside recursion). For very large numbers, build results with StringBuilder or accumulate bits in a list then join for performance. Decide how negative inputs should be handled (sign bit, two's-complement, or reject). This explanation ties back to ’s original question and clarifies the confusion hinted at by .

Recommended Answers

All 3 Replies

Hi i found this code on the web that works but can someone tell my how does this works


Private Function RecursionFunc(ByRef AsciiNum As Short, ByRef SubNum As Short) As Object

If AsciiNum - SubNum > 0 Then
txtNew.Text = txtNew.Text & 1 'Add a 1 to indicate that it contains a subnum in it
If SubNum <> 1 Then 'Check to make sure it's not 1
RecursionFunc(AsciiNum - SubNum, SubNum / 2) 'Call myself with the new asciinum and subnum
End If
Else
txtNew.Text = txtNew.Text & 0 'Add a zero to indicate that the subnum is not contained
If SubNum <> 1 Then 'Check to make sure it's not 1
'Call myself with the same asciinum because it does not contain a subnum and make subnum the next smallest
RecursionFunc(AsciiNum, SubNum / 2)
End If
End If

Hi i found this code on the web that works but can someone tell my how does this works

u said it works but you want to someone tell u how it works?confusing statement.

yes how does this code works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.