Knowing how to convert binary numbers to their decimal equivalent is an important skill to learn when coding in a programming language such as Visual Basic 6 (VB6). Unlike the decimal system which has digits from 0 to 9, the binary system only has digits for 0 and 1. Each place in a binary number represents a power of 2. There is no built-in function to convert a binary number string so you must create a function that calculates it manually.
- Skill level:
Other People Are Reading
Open your VB6 source file in an editor such as Microsoft Visual Basic 6.
Create a function that will convert a binary number into decimal form by adding the code:
Public Function ConvBinaryToInteger(ByVal MyBinNumber As String) As Long
Dim n As Integer,
Dim temp As Long
For n = 1 To Len(MyBinNumber)
Temp = Temp + Val(Mid(MyBinNumber, Len(MyBinNumber) - n + 1, 1))* 2 ^ (n - 1)
ConvBinaryToInteger = Temp
The function takes each individual binary digit from right to left, multiplies it by 2 raised to the appropriate power and then adds that value to the cumulative sum. For example, "110" is processed as "02^0 + 12^1 + 1*2^2 = 6."
Store the binary number you want to convert in a string by adding the code:
Dim binstr As String = "110";
Call the function to convert a binary value, by adding the code:
Dim result As Long
result = ConvBinaryToInteger(binstr.Text)
The variable "result" contains the value of the binary conversion, which in the example is 6.
Save the VB6 file, compile and run the program to convert the binary value.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for