Convert Binary to Decimal and vice versa

Convert Binary to Decimal and vice versa

//Function which converts decimal to binary
Private Function Dec2Bin(ByVal dec As Double) As String
   Dec2Bin = “”
   While dec >= 1
     Dec2Bin = IIf(dec Mod 2 = 0, “0″, “1″) & Dec2Bin
     dec = dec / 2
   Wend
End Function

//Function which converts binary to decimal
Private Function Bin2Dec(ByVal str As String) As Double
   Dim x As Integer
   Bin2Dec = 0
   For x = 1 To Len(str)
     Bin2Dec = Bin2Dec + (CInt(Mid(str, x, 1)) * (2 ^ (Len(str) - x)))
   Next x
End Function

Leave a Reply


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.