Results 1 to 7 of 7

Thread: AAAH!!! i'm running out of time!!!

  1. #1

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

    AAAH!!! i'm running out of time!!!

    ok so i have this...

    VB Code:
    1. Option Explicit
    2. Dim strNewNum As String
    3.  
    4. Function DecimalToBinary(ByVal lngBin As Long, lngDesc As Long) As Long
    5.     Dim intNum As Integer
    6.    
    7.     Dim lngBinary2 As Long
    8.     Dim lngDecimal As Long
    9.     Dim lngStepping As Long
    10.     Dim strDigit As String
    11.    
    12.    
    13.     intNum = txtNum.Text
    14.     lngStepping = 1
    15.    
    16.     Do Until intNum \ lngStepping = 1
    17.         lngStepping = lngStepping * 2
    18.     Loop
    19.     Do Until intNum <= 0
    20.         strDigit = intNum \ lngStepping
    21.         strNewNum = strNewNum & strDigit
    22.         intNum = intNum Mod lngStepping
    23.         lngStepping = lngStepping / 2
    24.     Loop
    25. End Function
    26.  
    27. Private Sub cmdBtoD_Click()
    28.    
    29. End Sub
    30.  
    31. Private Sub cmdDone_Click()
    32.     Unload Me
    33. End Sub
    34.  
    35. Private Sub cmdDtoB_Click()
    36.     Dim lngBinary As Long
    37.     lngBinary = DecimalToBinary(1, 65000)
    38.     MsgBox (strNewNum)
    39. End Sub

    it all works...

    now how do you convert the binary back to decimal? (no case plz i havn't learned them)
    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
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Each digit in a binary value represents a power of 2, starting with 2^0 for the first (right-most) binary digit, 2^1 for the second digit, and so on. Note that any number to the 0 power is by definition 1, and any number to the 1 power is the number itself. Using 1101 for another example, you have:

    The first digit is 1, and 1 times 2^0 is 1.
    The second digit is 0, and 0 times 2^1 is 0.
    The third digit is 1, and 1 times 2^2 is 4.
    The fourth digit is 1, and 1 times 2^3 is 8.
    8 + 4 + 1 equals 13.

    A function to perform the binary to decimal conversion is shown here.

    VB Code:
    1. Public Function BinaryToDecimal(BinaryValue As String) As Long
    2.  
    3.  ' Returns the decimal equivalent of a binary number.
    4.  
    5. Dim idx As Integer
    6. Dim tmp As String
    7. Dim result As Long
    8. Dim digits As Integer
    9.  
    10. digits = Len(BinaryValue)
    11. For idx = digits To 1 Step -1
    12.     tmp = Mid(BinaryValue, idx, 1)
    13.     If tmp = "1" Then result = result + 2 ^ (digits - idx)
    14. Next
    15.  
    16. BinaryToDecimal = result
    17.  
    18. End Function

    This function treats any character other than "1" in the binary value as a "0". You might want to add error-schecking code to ensure that the binary value contains only "0" and "1" characters.

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171


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

  4. #4

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

    new one..

    ok here is what i have

    VB Code:
    1. Function BinaryToDecimal(ByVal lngBin As Long, ByVal lngDesc As Long) As Long
    2.     Dim strNum As String
    3.     Dim lngNum As Long
    4.     Dim lngCounter As Long
    5.    
    6.     strNum = txtNum.Text
    7.    
    8.     lngCounter = Len(strNum)
    9.    
    10.    
    11.    
    12.     Do Until lngCounter = 0
    13.         lngNum = 2 ^ lngCounter
    14.         strDecimal = strDecimal & lngNum
    15.         lngCounter = lngCounter - 1
    16.     Loop
    17.    
    18. End Function

    now for some reason th emsg box comes back as (say if the decimal number is 513... converts it into 1000000001 the msg box will come back as 1024512256128643216842) how can i add thos enumbers without converting it into a long??? or do i have to?
    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!!!

  5. #5

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

    hmmm

    VB Code:
    1. Function BinaryToDecimal(ByVal strBin As String) As Long
    2.     Dim strNum As String
    3.     Dim lngNum As Long
    4.     Dim lngCounter As Long
    5.     Dim lngHelper As Long
    6.    
    7.     strNum = txtNum.Text
    8.    
    9.     lngCounter = Len(strNum) - 1
    10.        
    11.     Do Until lngCounter < 0
    12.         lngNum = 2 ^ lngCounter
    13.         lngHelper = lngHelper + lngNum
    14.         lngCounter = lngCounter - 1
    15.         strDecimal = lngHelper
    16.     Loop
    17.    
    18. End Function

    ok now it says if i convert 513 to binary and then i convert it back... i get an answer of 1023... help??? (i can't dl zip files on my school computer)
    Last edited by MiddleAged1; Mar 12th, 2004 at 10:33 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!!!

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Input binary and output binary ought to be stored as a string.

    Given 10110101, StrReverse the string so you go left to right (1 to Len())

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.    Text2.Text = BinaryToDecimal(Text1.Text)
    5. End Sub
    6.  
    7. Private Sub Command2_Click()
    8.    Text3.Text = DecimalToBinary(Val(Text2.Text))
    9. End Sub
    10.  
    11. Public Function BinaryToDecimal(strInputBits As String) As Long
    12. Dim strRevBinaryBits As String
    13. Dim lngDecimal As Long
    14. Dim lngX As Long
    15.  
    16.    strRevBinaryBits = StrReverse(strInputBits)
    17.    lngDecimal = 0
    18.    For lngX = 1 To Len(strRevBinaryBits)
    19.       If Val(Mid(strRevBinaryBits, lngX, 1)) > 0 Then
    20.          lngDecimal = lngDecimal + 2 ^ (lngX - 1)
    21.       End If
    22.    Next
    23.    
    24.    BinaryToDecimal = lngDecimal
    25. End Function
    26.  
    27. Public Function DecimalToBinary(lngInputLong As Long) As String
    28. Dim strTempBits As String
    29.  
    30.    strTempBits = ""
    31.    Do While lngInputLong > 0
    32.       strTempBits = strTempBits & (lngInputLong Mod 2)
    33.       lngInputLong = lngInputLong \ 2
    34.    Loop
    35.    
    36.    DecimalToBinary = StrReverse(strTempBits)
    37. End Function
    Last edited by leinad31; Mar 12th, 2004 at 10:47 AM.

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Just in case...

    VB Code:
    1. Public Function EnsureBinaryString(strInput As String) As String
    2. Dim lngX As Long
    3. Dim strTemp As String
    4.  
    5.    strTemp = ""
    6.    For lngX = 1 To Len(strInput)
    7.       If Mid(strInput, lngX, 1) = "1" Or Mid(strInput, lngX, 1) = "0" Then
    8.          strTemp = strTemp & Mid(strInput, lngX, 1)
    9.       End If
    10.    Next
    11.    
    12.    EnsureBinaryString = strTemp
    13. End Function

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