Converting Decimal to Binary VB2010

Post here to discuss programming in VB.net

Moderators: Chuckt, Garth, bitfogav

Post Reply [phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Converting Decimal to Binary VB2010

Post by bitfogav » Fri Nov 04, 2011 4:02 am

I was looking for a way to convert a Decimal number to Binary in VB 2010. So wrote the following function.

It will convert decimal integer values to a binary string, you also have the option to set the number of bits the function returns, by default the function will return 8 bits.

For example:

Code: Select all

Dim myDec As Integer

myDec = 146

textbox1.Text = DecToBin(myDec)

' will return Binary 10010010

Or we could set the number of bits to return like this

Code: Select all

Dim myDec As Integer

myDec = 146

textbox1.Text = DecToBin(myDec, 16)

' will return Binary 0000000010010010

Code: Select all

   Public Function DecToBin(ByVal DeciValue As Long, Optional ByVal NoOfBits As Integer = 8) As String
        Dim i As Integer 
        Do While DeciValue > (2 ^ NoOfBits) - 1
            NoOfBits = NoOfBits + 8
        Loop
        DecToBin = vbNullString 
        For i = 0 To (NoOfBits - 1)
            DecToBin = CStr((DeciValue And 2 ^ i) / 2 ^ i) & DecToBin
        Next i
    End Function

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Converting Decimal to Binary VB2010

Post by bitfogav » Fri Nov 04, 2011 7:48 am

Just incase you want to convert a Binary number back to Decimal:

Code: Select all

    ' builds a decimal number from a binary string. 
    Public Function BinToDec(ByVal Binary As String) As Long
        Dim n As Long
        Dim s As Integer

        For s = 1 To Len(Binary)
            n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ (s - 1)))
        Next s

        BinToDec = n
    End Function

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Converting Decimal to Binary VB2010

Post by brad » Mon Nov 07, 2011 11:50 am

very handy!

I am surprised it doesn't have that sort of functionality built in?

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Converting Decimal to Binary VB2010

Post by bitfogav » Tue Nov 08, 2011 5:20 am

brad wrote: I am surprised it doesn't have that sort of functionality built in?

I spent quite awhile looking for a function in VB for Binary conversions but no luck!

Found some for Decimal to HEX though :)

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Converting Decimal to Binary VB2010

Post by brad » Sat Nov 12, 2011 6:10 am

Speaking of conversions - I have been giving lectures on that sort of thing just this week :D

Post Reply
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Who is online

Users browsing this forum: No registered users and 2 guests