Search content:

 

Personal Menu
Username:
Password:
Save password

Become a member

Forgot Password?

 

Don't miss these
preloadNetThing Behavior
All Purpose Status Bar
Blur Range/Pendulum-Alphamania
Light Up Game Controller
Universal Movie Pause Button
EZ CD Creator
Device Xtra
Cinemac
MM-XDK-L (Developers of Xtras)
China Multimedia Union
MediaMacros Xtras Mall
 

 

 

Behavior integer to binary and binary to integer

Added on 3/11/2002

 

Compatibilities:
D7 D8 D8_5 D9 Shockwave

This item has not yet been rated

Author: mango

Two handlers that converts integers to binary strings and binary strings to integers without xtras. It came up when I instead of a progressbar wanted to show progress with a binary string. The final project used a standard progressbar:-) /Mango

--usage----
--put intToBin(65,8) returns a eight digit binary string
-- "01000001"

--put intToBin(65,0) returns a binary string without zeropadding
-- "1000001"

--errorcodes
-- -1 = integer value > max possible value with wanted number of digits
-- -2 = integer value < 0
-- -3 = integer not an integer
-- -4 = binbase not an integer

on intToBin int, binbase
  --errorchecking
  if integerp(binbase) = 0 then return -4
  if integerp(int) = 0 then return -3
  if int < 0 then return -2
  if binbase > 0 and integer(power(2,binbase)) > int = 0 then return -1
  --end errorchecking
  --calculate automatic binbase if binbase = 0
  if binbase = 0 then
    tint = int
    binbase = 1
    repeat while tint > 1
      tint = tint / 2
      binbase = binbase +1
    end repeat
  end if
  --end automatic binbase calculation
  repeat with i = binbase down to 1
    base = integer(power(2,i-1))
    a = int / base
    int = int - (a*base)
    s = s & a
  end repeat
  return s
end


--usage----
--put binToInt("1000001")
-- 65

--errorcodes
-- -1 = string contains digits other than 0 and 1
-- -2 = string is not a string

on binToInt abin
  if stringp(abin) = 0 then return -2
  tmpr = 1
  tint = 0
  repeat with i = 1 to abin.char.count
    b = abin.char[abin.char.count -i +1]
    case b of
      "1":
        tint = tint + tmpr
      "0":
      otherwise
        return -1
    end case
    tmpr = tmpr *2
     end repeat
  return tint
end

 


Upload Provided by ABCUpload ASP

Contact

MMI
22 West Court Sq
Suite 2C
Newnan, GA 30263
USA

Fax - (206) 339-5833

Send e-mail