Click to See Complete Forum and Search --> : How can I convert a decimal number to a binary number?


kevin shen
May 6th, 2001, 05:57 PM
How can I convert a decimal number to a binary number? Do you know if there's the function I can find in VB?

Thank you for your help!

Best Regards,

Kevin Shen

udipr
May 7th, 2001, 01:24 AM
I don't think there is any function in VB, But I wrote function that do it.
Try it:

private Function DecimalToBinary(decNumber as Long) as string
Dim i as Long
Dim binNumber as string
binNumber = ""
i = 1
Do While decNumber > 0
If ((decNumber \ i) Mod 2) = 1 then
binNumber = "1" & binNumber
decNumber = decNumber - i
else
binNumber = "0" & binNumber
End If
i = i * 2
Loop
DecimalToBinary = binNumber
End Function




I hope it help you.

Iouri
May 7th, 2001, 07:24 AM
Here's a function that will convert any decimal number less than 256 to binary:

Function Decimal2Binary(TheNumber As Integer) As String
Dim TheBinaryNumber As String
Dim ThePosition As Integer
Dim iWork As Integer
TheBinaryNumber = "00000000"
If TheNumber < 256 Then
ThePosition = 128
For iWork = 1 To 8
If TheNumber >= ThePosition Then
Mid$(TheBinaryNumber, iWork, 1) = "1"
TheNumber = TheNumber - ThePosition
End If
ThePosition = ThePosition / 2
Next
End If
Decimal2Binary = TheBinaryNumber
End Function



Iouri Boutchkine
iouri@hotsheet.com

Johnny101
May 7th, 2001, 02:45 PM
here's an article that has a routine that will go from any base number to any other base number and back again. http://www.codeguru.com/vb/articles/2137.shtml

hope it can help,

john

John Pirkey
MCSD
http://www.ShallowWaterSystems.com
http://www.stlvbug.org