Results 1 to 8 of 8

Thread: must be an easier way...[Resolved] figured it out myself!

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2004
    Location
    one of the coldest and hottest places in the world... :::HINT::: canada
    Posts
    42

    must be an easier way...[Resolved] figured it out myself!

    VB Code:
    1. Function DecimalToBinary(ByVal lngBin As Long, lngDesc As Long) As Long
    2.     Dim intNum As Integer
    3.    
    4.     Dim lngBinary2 As Long
    5.     Dim lngDecimal As Long
    6.    
    7.     intNum = txtNum.Text
    8.    
    9.     For intNum = intNum To 255
    10.         If intNum >= 2 ^ 7 Then
    11.             strNewNum = strNewNum & "1"
    12.             intNum = intNum - 2 ^ 7
    13.         Else
    14.             strNewNum = strNewNum & "0"
    15.         End If
    16.         If intNum >= 2 ^ 6 Then
    17.             strNewNum = strNewNum & "1"
    18.             intNum = intNum - 2 ^ 6
    19.         Else
    20.             strNewNum = strNewNum & "0"
    21.         End If
    22.         If intNum >= 2 ^ 5 Then
    23.             strNewNum = strNewNum & "1"
    24.             intNum = intNum - 2 ^ 5
    25.         Else
    26.             strNewNum = strNewNum & "0"
    27.         End If
    28.         If intNum >= 2 ^ 4 Then
    29.             strNewNum = strNewNum & "1"
    30.             intNum = intNum - 2 ^ 4
    31.         Else
    32.             strNewNum = strNewNum & "0"
    33.         End If
    34.         If intNum >= 2 ^ 3 Then
    35.             strNewNum = strNewNum & "1"
    36.             intNum = intNum - 2 ^ 3
    37.         Else
    38.             strNewNum = strNewNum & "0"
    39.         End If
    40.         If intNum >= 2 ^ 2 Then
    41.             strNewNum = strNewNum & "1"
    42.             intNum = intNum - 2 ^ 2
    43.         Else
    44.             strNewNum = strNewNum & "0"
    45.         End If
    46.         If intNum >= 2 ^ 1 Then
    47.             strNewNum = strNewNum & "1"
    48.             intNum = intNum - 2 ^ 1
    49.         Else
    50.             strNewNum = strNewNum & "0"
    51.         End If
    52.         If intNum >= 2 ^ 0 Then
    53.             strNewNum = strNewNum & "1"
    54.             intNum = intNum - 2 ^ 0
    55.         Else
    56.             strNewNum = strNewNum & "0"
    57.         End If
    58.         intNum = 256
    59.     Next intNum
    60. 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!!!

  2. #2
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310
    Try this one

    VB Code:
    1. Public Function Dec2Bin(ByVal DecNum As Long) As String
    2.     Select Case DecNum
    3.         Case Is > 1
    4.         Dec2Bin = Dec2Bin(DecNum \ 2) & CStr(DecNum And 1)
    5.         Case Is > 0
    6.         Dec2Bin = "1"
    7.         Case Else
    8.         Dec2Bin = "0"
    9.     End Select
    10. End Function

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2004
    Location
    one of the coldest and hottest places in the world... :::HINT::: canada
    Posts
    42
    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!!!

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    I have a post for this in the codebank


    Has someone helped you? Then you can Rate their helpful post.

  5. #5
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310
    Not that it matters because you found a solution, but what did you have against the case statements?

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2004
    Location
    one of the coldest and hottest places in the world... :::HINT::: canada
    Posts
    42
    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!!!

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    This works for me. It converts long integers:
    VB Code:
    1. Private Function Dec2Bin(ByVal lngNo As Long) As String
    2.     ' This functin converts a variable of type long
    3.     ' to its 32 bit signed binary representation
    4.    
    5.     Dim strBin As String
    6.     Dim count As Integer
    7.     Dim bit As Byte
    8.    
    9.     ' if its zero - no point in converting it
    10.     If lngNo = 0 Then
    11.         Dec2Bin = 0
    12.         Exit Function
    13.     End If
    14.    
    15.     If lngNo > 0 Then
    16.         ' postive numbers are easy
    17.         ' repeatedly divide by 2 until number is 0 using mod and integer binary division
    18.         While (lngNo <> 0)
    19.             bit = lngNo Mod 2
    20.             lngNo = lngNo \ 2
    21.        
    22.             strBin = bit & strBin
    23.         Wend
    24.     Else
    25.         ' if number is negitive convert it to twos compliment
    26.         ' firstly take one from negitive number and convert to positive
    27.         ' then produce a 32 bit number
    28.         lngNo = Abs(lngNo + 1)
    29.        
    30.         For count = 31 To 0 Step -1
    31.             If lngNo >= 2 ^ count Then
    32.                 strBin = strBin & "0"
    33.                
    34.                 lngNo = lngNo - 2 ^ count
    35.             Else
    36.                 strBin = strBin & "1"
    37.             End If
    38.         Next
    39.     End If
    40.    
    41.     Dec2Bin = strBin
    42. End Function
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8
    Hyperactive Member
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    278
    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
  •  



Click Here to Expand Forum to Full Width