On 28/4/04 12:23 pm, "2702NET" <[EMAIL PROTECTED]> wrote:

> Anyone have code for decimal to binary conversion you wouldn't mind
> posting?

Hi J,

This is about 30% faster than other leading brands :-)

Cheers

James

----------------------------------------------------------------------------


on DecimalToBitString(anInteger) -------------------------------------
  -- INPUT: <anInteger> can be any positive or negative integer
  -- OUTPUT: Returns a 32-character string of 0s and 1s corresponding
  --         to the binary representation of <anInteger>
  --------------------------------------------------------------------
  
  if anInteger < 0 then
    tBitString = DecimalToBitString(the  maxInteger + 1 + anInteger)
    put "1" into char 1 of tBitString
    return tBitString
  end if
  
  tBitString = ""
  tZeros  = 32
  
  repeat while anInteger
    put anInteger mod 2 before tBitString
    anInteger = anInteger / 2
    tZeros    = tZeros - 1
  end repeat
  
  -- Comment out the following lines to return a short string of
  -- 1s and 0s if you are working with positive numbers only
  repeat while tZeros
    tBitString = "0"&tBitString
    tZeros     = tZeros - 1
  end repeat
  --
  
  return tBitString
end DecimalToBitString



on BitStringToDecimal(aBitString) -------------------------------------
  -- INPUT: <aBitString> should be a string of 1s and 0s
  -- OUTPUT: the integer corresponding to the binary number in
  --         <aBitString>
  --------------------------------------------------------------------

  tBaseTen = 0
  tBits    = length (aBitString)
  
  repeat with i = 1 to tBits
    tBaseTen = tBaseTen * 2
    if (char i of aBitString = "1") then
      tBaseTen = tBaseTen + 1
    end if
  end repeat
  return tBaseTen
end BitStringToDecimal

[To remove yourself from this list, or to change to digest mode, go to 
http://www.penworks.com/lingo-l.cgi  To post messages to the list, email [EMAIL 
PROTECTED]  (Problems, email [EMAIL PROTECTED]). Lingo-L is for learning and helping 
with programming Lingo.  Thanks!]

Reply via email to