Results 1 to 13 of 13

Thread: Binaries

  1. #1

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Unhappy

    Urgent. How do i convert a decimal to binaries and vice versa? Also, how can i fiund the high and low bit of the binary?
    Amon Ra
    The Power of Learning.

  2. #2
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    Check out this thread:

    http://forums.vb-world.net/showthrea...6&goto=newpost

    The question is answered quite well there.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  3. #3
    Guest
    Code:
    Function Bin(ByVal sBinary As String) As Long
        Dim I As Integer
        For I = 0 To Len(sBinary) - 1
        If Mid(sBinary, Len(sBinary) - I, 1) = "1" Then Bin = Bin + (2 ^ I)
        Next
    End Function
    Usage:
    Code:
    Print Bin("110001010101010001011011")

  4. #4

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Wink ok

    in the following:

    code:--------------------------------------------------------------------------------

    1-Private Function Dec2Bin(Num As Integer) As String
    2- Dim i As Integer
    3- Dec2Bin = ""
    4- For i = 0 To 7
    5- If Num And 2 ^ i Then
    6- Dec2Bin = "1" + Dec2Bin
    7- Else
    8- Dec2Bin = "0" + Dec2Bin
    9- End If
    10- Next i

    End Function

    what does the line 5 do?
    Amon Ra
    The Power of Learning.

  5. #5

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Unhappy Also

    the function you, megatron, gave me does not work for me

    if i convert 2 to binaries, it returns 00000010
    but if i try to go from bin. to dec. with your function with
    00000101, it returns 14 instead of 5 . i dont get it
    Amon Ra
    The Power of Learning.

  6. #6
    Guest
    2 is 00000010 in binary.

  7. #7
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    OK, in decimal:

    1234 is broken up like this: 1 thousand, 2 hundreds, 3 tens, and 4 ones. In binary, it works like this:

    1111 is 1 one, 1 two, 1 four, and 1 eight, or 15, while 1110 is no ones, 1 two, 1 four, and 1 eight, or 14. Line 5 in that code checks to see whether the next digit it needs to write is a 0 or a 1.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  8. #8
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226

    Megatrons function

    Sorry, Meggie, but I just had to say, do your homework.

    Amon ra: the code should read as below:

    Code:
    Public Function Bin2Dec(Byval Bin As String) As Long
     Dim I As Integer, RetVal As Long
     For I=Len(Bin) To 1 Step -1
      If Mid$(Bin, I, 1)="1" Then RetVal=RetVal+(2^(I-1))
     Next I
    End Function
    This should work (quite) a bit better.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  9. #9
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    here is something i threw together a few min ago.
    Code:
    Function dec2bin(numtoconvert As Long) As String
       Dim worktext As String
       Dim cl As Long
       Dim workval As Long
       worktext = Oct(numtoconvert)
       For cl = 1 To Len(worktext)
           workval = Val(Mid(worktext, cl, 1))
          If (workval And 4) = 4 Then output = output + "1" Else output = output + "0"
          If (workval And 2) = 2 Then output = output + "1" Else output = output + "0"
          If (workval And 1) = 1 Then output = output + "1" Else output = output + "0"
       Next cl
       dec2bin = output
    End Function
    
    Function bin2dec(text As String) As Long
       Dim multiplier As Double
       Dim cl As Long
       multiplier = 1
       Dim output As Double
       For cl = Len(text) To 1 Step -1
          output = output + (Val(Mid$(text, cl, 1)) * multiplier)
          multiplier = multiplier + multiplier
       Next cl
       bin2dec = output
    End Function
    I hope you don't have any problem decoding HOW they work. Just be happy that they do work
    Last edited by Lord Orwell; Apr 1st, 2001 at 04:26 AM.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  10. #10

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Cool :)

    Thanks guys. Now i have to decide which one to use
    Amon Ra
    The Power of Learning.

  11. #11
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    you have to use mine!
    Im the only one who gave a converstion TO binary.

    converting FROM is the easy part...
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  12. #12
    Guest

    Re: Megatrons function

    Originally posted by mlewis
    Sorry, Meggie, but I just had to say, do your homework.
    I beg to differ, but my function does work.

    Try this (in a Module)
    Code:
    Function Bin(ByVal sBinary As String) As Long
        Dim I As Integer
        For I = 0 To Len(sBinary) - 1
        If Mid(sBinary, Len(sBinary) - I, 1) = "1" Then Bin = Bin + (2 ^ I)
        Next
    End Function
    Now place this line in a CommandButton and it outputs 5 (not 14).
    Code:
    Print Bin("00000101")

  13. #13

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Talking HAHAHA

    OK, your functions all work Thx
    Amon Ra
    The Power of Learning.

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