# How to Convert a Binary Value in Visual Basic 6

Written by sean mann
• Share
• Tweet
• Share
• Pin
• Email

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:
Moderate

## Instructions

1. 1

Open your VB6 source file in an editor such as Microsoft Visual Basic 6.

2. 2

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)
``````

Next

ConvBinaryToInteger = Temp

End Function

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."

3. 3

Store the binary number you want to convert in a string by adding the code:

Dim binstr As String = "110";

4. 4

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.

5. 5

Save the VB6 file, compile and run the program to convert the binary value.

### Don't Miss

#### References

• All types
• Articles
• Slideshows
• Videos
##### Sort:
• Most relevant
• Most popular
• Most recent