0

I have a problem converting the integer number to binary number, anyone can guide me on which one I get the error in my coding? Thanks.

Imports System.Text

Module Module1

    Sub Main()
        Dim number As Integer
        Console.Write("Please Enter Number: ")
        number = Console.ReadLine()

        'Print the results
        Console.WriteLine("The binary is: " & ConvertDecimalToBinary(number))

    End Sub

    Private Function ConvertDecimalToBinary(number As Integer) As String

        Dim remainder As Integer
        Dim num As Integer

        'Create a string for binary
        Dim sb = New StringBuilder()
        Do
            remainder = number Mod 2
            sb.Insert(0, remainder)
            num \= 2
        Loop While num = 1


        Return remainder
    End Function



End Module

This is my output when I insert number 5 then give me the binary number is 1: Output

I want the output like below the sample picture:

Output2

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • this is obviously VB.Net, not C#. I fix the tags. – Christopher Mar 08 '20 at 12:07
  • Generally you will need two loops. One counting up until it found a multiple of 2 just below the input. And one that keeps substracting and dividing downward. – Christopher Mar 08 '20 at 12:08
  • Does this answer your question? [How to convert integer to binary string in C#?](https://stackoverflow.com/questions/3702216/how-to-convert-integer-to-binary-string-in-c) – Poul Bak Mar 08 '20 at 12:16
  • Thanks Christopher. Can you show me how to make it in two loops? –  Mar 08 '20 at 12:17
  • 1
    You're assigning a string to a variable declared as Integer. Do yourself a favor and set `Option Strict On`. Also, when accepting User input, you need to use `TryParse()`. (e.g, `if Integer.TryParse(Console.ReadLine(), number) then 'Call the method else ' Input error end if`) – Jimi Mar 08 '20 at 13:19
  • `Return remainder` - are you sure? You've just carefully made a string in `sb` ;) – Andrew Morton Mar 08 '20 at 13:56

2 Answers2

0

EDIT : There is a more simple solution, here you go :

Sub Main()
    Dim number As Integer
    Console.Write("Please enter a number : ")
    number = Convert.ToInt32(Console.ReadLine)

    'Print the results
    Console.WriteLine("The binary result of the number is : " & Convert.ToString(number, 2))
    Console.ReadKey()
End Sub

This is the translated code from C# to VB from this post :

Private Function ConvertDecimalToBinary(number As Integer) As String
    Dim bits As Char() = New Char(32) {}
    Dim i As Integer

    While number <> 0
        bits(i) = If((number And 1) = 1, "1"c, "0"c)
        i += 1
        number >>= 1
    End While

    Array.Reverse(bits, 0, i)
    Return New String(bits)
End Function

So your main function would be something like this :

Sub Main()
    Dim number As Integer
    Console.Write("Please enter a number : ")
    number = Convert.ToInt32(Console.ReadLine)

    'Print the results
    Console.WriteLine("The binary result of the number is : " & ConvertDecimalToBinary(number))
    Console.ReadKey()
End Sub
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
ShiningLea
  • 132
  • 1
  • 12
0

There is an easier way, you can try it.https://learn.microsoft.com/en-us/dotnet/api/system.bitconverter?redirectedfrom=MSDN&view=netframework-4.8

Sub Main()
    Dim number As Integer
    Console.Write("Please Enter Number: ")
    number = Console.ReadLine()

    'Print the results

    Console.WriteLine("The binary is: " & Convert.ToString(number, 2))
    Console.ReadKey()
End Sub
Julie Xu-MSFT
  • 334
  • 1
  • 5