Results 1 to 3 of 3

Thread: Quick Question ...

  1. #1

    Thread Starter
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908

    Quick Question ...

    In the General forums, I attempted to help someone who was looking for a way to change a double into a binary string. I have one that works up to 2^31 or 999,999,999. Is there any possible way to convert numbers greater than that to binary? Any ideas would be appreciated.

    I know, he should have posted here if he really wanted to know, but it's been bugging me for the past couple of days

    Thankee
    -Excalibur

  2. #2

    Thread Starter
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    I found an answer on planet-source-code.com here it is (modified by me to be binary only and return the correct number of paired 1's and 0's) I didn't catch the original authors name, but kudos to him as well.
    Code:
    Function ConvertToBase(DecNumber As Double) As String
        Dim ModBase As Double
        Do
            ModBase = CDbl(DecNumber - (Int(DecNumber / 2)) * 2)
            DecNumber = Int(DecNumber / 2)
            If ModBase > 9 Then ModBase = ModBase + 7
            ConvertToBase = Chr(ModBase + 48) & ConvertToBase
        Loop Until DecNumber = 0
        If Len(ConvertToBase) / 2 <> Len(ConvertToBase) \ 2 Then ConvertToBase = "0" & ConvertToBase
    End Function
    -Excalibur

  3. #3
    Lively Member TB's Avatar
    Join Date
    Feb 2001
    Location
    Austria
    Posts
    106
    To convert a big decimal number into a binary is not too difficult, but you will have problem to do that backward.

    The input is a string, that contains the number (in VB, you can't use a number with - let's say - 300 digits)

    1)
    Then you have to check whether the number(string) is divisble by 2. You only have to check the last digit.
    If this is true, the first digit of your binary number is 1, else, it's 0.

    2)
    Then, the problem appears, you'll have to divide this string by 2.
    So, you need a loop, that does that for you.
    If you managed to divide the number, goto 1) and repeat this until
    the number is 0. (that means, the last division is 1/2 or 2/2).
    mojo

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