Question a console to convert integer to binary format

kaisermhi

Member
Joined
Sep 22, 2010
Messages
8
Programming Experience
Beginner
how to write a console application which can use the integer as an input and output the corresponding value as a binary.
 
you should start with checking the msdn page for Int32:

Int32 Structure (System)

scroll to the bottom, you'll see something about binary (just cntrl+F and type in binary)

remember binary is just a way to 'spell' a numeric value. So it's to do with ToString... in it you'll see there is a couple ways to do it, probably the easiest is:

Convert.ToString( myValue, 2 ) ''2 for binary
Convert.ToString( myValue, 8 ) ''8 for octal
Convert.ToString( myValue, 16 ) ''16 for hexadecimal
 
need more info

thanks.but I am looking for a application which can show how to show the conversion by using If..else..End if...
 
Hmmm, i'm sure there are a few ways to do this, but one initial thought would be to subtract the binary values from the integer as you go...

If you worked with 8 bits, the last (most significant bit) has a value of 128 , so if you had an integer of 200...

200 (does 128 fit? if yes, take it away)

200-128 = 72
1

72 - 64 = 8

11

8 - 32 = nope

110

8 - 16 = nope

1100

8-8 = 0

11001

0-4 = nope

110010

0-2 = nope

1100100

0-1 = nope

11001000



Not sure if that makes sense, infact I only checked it with 200, obviously with numbers bigger than 8 bits you will need to move to 16 bit etc


Theres probably better ways to do it, but in code, that would be a case of looping the amount of bits, and then seeing what that individual bit is worth and seeing if it fits into the integer.

Hope it helps...
 
Manual conversion from decimal to binary output

Here is a Console program that shows how to convert an integer to display as a binary number, up to 4 bytes. Keep in mind that the binary display is a string. There is a string method that does the conversion in one line. I included that at the end so you can compare the two results.

VB.NET:
    Sub Main()
        Dim N, x, byt, decnum As Integer
        Dim binum As String = "", sdec As String
        Dim p As Long
        Console.Write("Enter decimal number:  ")
        sdec = Console.ReadLine()
        If Integer.TryParse(sdec, decnum) = False Then
            Console.WriteLine("Too big")
            Exit Sub
        End If
        byt = 4
        N = 8 * byt     'maximum of 4 bytes
        p = 1           'place value
        For x = 0 To N
            If (decnum And p) = 0 Then
                binum = "0" & binum    'concatenate the string
            Else
                binum = "1" & binum
            End If
            p = p + p      'double the place value
        Next x
        binum = binum.TrimStart("0"c)   'remove leading 0s to shorten display
        Console.WriteLine()     'using manual conversion
        Console.WriteLine(binum)
        Console.WriteLine()     'using the Convert method:
        Console.WriteLine(Convert.ToString(decnum, 2))
        Console.ReadLine()
    End Sub
 
Last edited:
Dim Number As Integer = 200 ' the number to find in binary
Dim BitValue As Integer = 128 'most significant bit
Dim bits As Integer = 8 ' how many bits to use

Dim OutputString As String = Nothing

For x = 1 To bits

If BitValue <= Number Then

Number = Number - BitValue
BitValue = BitValue / 2
OutputString &= "1"

Else

OutputString &= "0"
BitValue = BitValue / 2
End If

Next


MsgBox(OutputString)

My primitive version :smile:
 
Back
Top