Results 1 to 2 of 2

Thread: decimal/binary convert - repetition functions

  1. #1

    Thread Starter
    Member
    Join Date
    May 2001
    Posts
    37

    decimal/binary convert - repetition functions

    im trying to make a binary/decimal convertor using repetition functions and no built in function. im having problems declaring the variables and using them in my algorithms. please help!!
    --dec to bin--
    set the bitstring to nothing
    for each bit required in the bitstring
    Calculate the remainder when the number is divided by 2
    Number = Number \ 2 (integer division)
    Bitstring = Remainder concatenated with the Bitstring
    next bit
    output the bitstring
    --dec to bin--
    set the bitstring to nothing
    Do
    Calculate the remainder when the number is divided by 2
    Number = Number \ 2 (integer division)
    Bitstring = Remainder concatenated with the Bitstring
    loop until Number is zero
    output the bitstring
    --bin to dec--
    Number = 0
    for each character in the bitstring
    Determine the value of the bit
    Number = Number * 2 + Value of the bit
    next character (moving rightwards through the bitstring)
    output the Number

  2. #2
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Here are two functions you can use:

    Code:
    Function BinToDec(ByVal BinString As Variant) As Double
     'Nucleus
     Dim BitValue As Double, LessABit As Double
     BinString = CDbl(BinString)
     BitValue = 1
     While BinString
        LessABit = BinString / 10
        If LessABit <> Int(LessABit) Then BinToDec = BitValue + BinToDec
        BitValue = BitValue * 2
        BinString = Int(LessABit)
     Wend
    End Function

    by Yonatan
    Code:
    Function Bin(ByVal X As Long) As String
        Do
            Bin = (X And 1) & Bin
            X = X \ 2
        Loop While X
    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