decimal to binary data in Visual Basic code

R

Thread Starter

Rafael Leal

Any one who knows how to convert a decimal value to a binary array in Visual Basic 6

The code is welcome....................
 
I can't remember where I got this function but this will convert a decimal to binary string without preceeding zeroes:

Public Function dec2bin(mynum As Variant) As String
Dim loopcounter As Integer
If mynum >= 2 ^ 31 Then
dec2bin = "Too big"
Exit Function
End If
Do
If (mynum And 2 ^ loopcounter) = 2 ^ loopcounter Then
dec2bin = "1" & dec2bin
Else
dec2bin = "0" & dec2bin
End If
loopcounter = loopcounter + 1
Loop Until 2 ^ loopcounter > mynum
End Function
 
This routine takes a decimal word value in "Value" and converts it to a boolean array (Attempted). You can use a Long and 0 to 31 if you want to decode a 32 bit word.

Dim n As Integer
Dim Attempted(0 To 15) As Boolean
Dim Value As Integer

For n = 0 To 15
If (Value And 2 ^ n) Then
Attempted(n) = True
Else
Attempted(n) = False
End If
Next
 
Top