|
-
Mar 10th, 2004, 10:10 AM
#1
Thread Starter
Member
must be an easier way...[Resolved] figured it out myself!
VB Code:
Function DecimalToBinary(ByVal lngBin As Long, lngDesc As Long) As Long
Dim intNum As Integer
Dim lngBinary2 As Long
Dim lngDecimal As Long
intNum = txtNum.Text
For intNum = intNum To 255
If intNum >= 2 ^ 7 Then
strNewNum = strNewNum & "1"
intNum = intNum - 2 ^ 7
Else
strNewNum = strNewNum & "0"
End If
If intNum >= 2 ^ 6 Then
strNewNum = strNewNum & "1"
intNum = intNum - 2 ^ 6
Else
strNewNum = strNewNum & "0"
End If
If intNum >= 2 ^ 5 Then
strNewNum = strNewNum & "1"
intNum = intNum - 2 ^ 5
Else
strNewNum = strNewNum & "0"
End If
If intNum >= 2 ^ 4 Then
strNewNum = strNewNum & "1"
intNum = intNum - 2 ^ 4
Else
strNewNum = strNewNum & "0"
End If
If intNum >= 2 ^ 3 Then
strNewNum = strNewNum & "1"
intNum = intNum - 2 ^ 3
Else
strNewNum = strNewNum & "0"
End If
If intNum >= 2 ^ 2 Then
strNewNum = strNewNum & "1"
intNum = intNum - 2 ^ 2
Else
strNewNum = strNewNum & "0"
End If
If intNum >= 2 ^ 1 Then
strNewNum = strNewNum & "1"
intNum = intNum - 2 ^ 1
Else
strNewNum = strNewNum & "0"
End If
If intNum >= 2 ^ 0 Then
strNewNum = strNewNum & "1"
intNum = intNum - 2 ^ 0
Else
strNewNum = strNewNum & "0"
End If
intNum = 256
Next intNum
End Function
there must be an easier way to do that without all those if statements... can some1 show me how plz?
Last edited by MiddleAged1; Mar 10th, 2004 at 10:38 AM.
I'm just a basic visual basic programmer!!! (no seriously... i'm just learning this stuff... the so called 'easy' stuff)... plz don't get mad at me if i don't know what i'm doing!!!
-
Mar 10th, 2004, 10:23 AM
#2
Hyperactive Member
Try this one
VB Code:
Public Function Dec2Bin(ByVal DecNum As Long) As String
Select Case DecNum
Case Is > 1
Dec2Bin = Dec2Bin(DecNum \ 2) & CStr(DecNum And 1)
Case Is > 0
Dec2Bin = "1"
Case Else
Dec2Bin = "0"
End Select
End Function
-
Mar 10th, 2004, 10:25 AM
#3
Thread Starter
Member
is there a way to do it without cases???
I'm just a basic visual basic programmer!!! (no seriously... i'm just learning this stuff... the so called 'easy' stuff)... plz don't get mad at me if i don't know what i'm doing!!!
-
Mar 10th, 2004, 10:26 AM
#4
-
Mar 10th, 2004, 10:41 AM
#5
Hyperactive Member
Not that it matters because you found a solution, but what did you have against the case statements?
-
Mar 10th, 2004, 10:47 AM
#6
Thread Starter
Member
i havn't learned them yet
I'm just a basic visual basic programmer!!! (no seriously... i'm just learning this stuff... the so called 'easy' stuff)... plz don't get mad at me if i don't know what i'm doing!!!
-
Mar 10th, 2004, 11:04 AM
#7
This works for me. It converts long integers:
VB Code:
Private Function Dec2Bin(ByVal lngNo As Long) As String
' This functin converts a variable of type long
' to its 32 bit signed binary representation
Dim strBin As String
Dim count As Integer
Dim bit As Byte
' if its zero - no point in converting it
If lngNo = 0 Then
Dec2Bin = 0
Exit Function
End If
If lngNo > 0 Then
' postive numbers are easy
' repeatedly divide by 2 until number is 0 using mod and integer binary division
While (lngNo <> 0)
bit = lngNo Mod 2
lngNo = lngNo \ 2
strBin = bit & strBin
Wend
Else
' if number is negitive convert it to twos compliment
' firstly take one from negitive number and convert to positive
' then produce a 32 bit number
lngNo = Abs(lngNo + 1)
For count = 31 To 0 Step -1
If lngNo >= 2 ^ count Then
strBin = strBin & "0"
lngNo = lngNo - 2 ^ count
Else
strBin = strBin & "1"
End If
Next
End If
Dec2Bin = strBin
End Function
-
Mar 10th, 2004, 05:20 PM
#8
Hyperactive Member
I havn't learned them yet
Why not learn them now
"Whether you think you can or cannot, you are always right."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|