Is there a Binary function available?

C

Cfox4

I have a need to convert decimal numbers to binary. I see that access has
built in functions for Hex and Octal conversions but not Binary. Am I
missing something? I'm trying to keep from having to write a major routine
to get this conversion.
 
D

Daniel

This comes from an old post by Randy Birch (MVP Visual Basic), you should be
able to adapt it to your needs.

'Decimal To Binary
' =================
' NOTE: You can limit the size of the returned
' answer by specifying the number of bits
Function Dec2Bin(ByVal DecimalIn As Variant, _
Optional NumberOfBits As Variant) As String
Dec2Bin = ""
DecimalIn = Int(CDec(DecimalIn))
Do While DecimalIn <> 0
Dec2Bin = Format$(DecimalIn - 2 * Int(DecimalIn / 2)) & Dec2Bin
DecimalIn = Int(DecimalIn / 2)
Loop
If Not IsMissing(NumberOfBits) Then
If Len(Dec2Bin) > NumberOfBits Then
Dec2Bin = "Error - Number exceeds specified bit size"
Else
Dec2Bin = Right$(String$(NumberOfBits, _
"0") & Dec2Bin, NumberOfBits)
End If
End If
End Function

'Binary To Decimal
' =================
Function Bin2Dec(BinaryString As String) As Variant
Dim X As Integer
For X = 0 To Len(BinaryString) - 1
Bin2Dec = CDec(Bin2Dec) + Val(Mid(BinaryString, _
Len(BinaryString) - X, 1)) * 2 ^ X
Next
End Function

the original post can be found at
http://groups.google.ca/group/comp....read/thread/28affecddaca98b4/979c5e918fad7e63

Daniel
 
L

Larry Linson

Cfox4 said:
I have a need to convert decimal numbers to binary.

For what purpose?

If not for display, Access stores all its data in one or another form of
binary.

If for display, then are you talking about the Decimal data type, or are you
talking about Integer, Long, Single-precision floating point,
Double-precision floating point, or Currency?
I see that access has built in functions for Hex and
Octal conversions but not Binary. Am I missing
something? I'm trying to keep from having to write
a major routine to get this conversion.

Larry Linson
Microsoft Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top