  title "Created By Mac"
  dialog CREATE,"Hex Dec Bin IP Converter",-1,0,250,180
  dialog ADD,STYLE,BoldText,MS Sans Serif,8,B
  dialog ADD,BUTTON,OK,60,5,140,30,,DEFAULT,BoldText
  dialog ADD,BUTTON,Clear,95,5,140,30,,BoldText
  dialog ADD,TEXT,Text1,8,5,,,"Entry:",BoldText
  dialog ADD,EDIT,Input,25,5,140,20,,BoldText
  dialog ADD,TEXT,Text2,133,5,,,"Result:",BoldText
  dialog ADD,EDIT,Output,150,5,240,20,,READONLY,BoldText
  dialog ADD,RADIO,Radio1,5,150,95,136," Conversion ",dec2hex|hex2dec|dec2bin|bin2dec|ip2bin|ASCIIcode,"dec2hex",CLICK,BoldText
  dialog SHOW

  list CREATE, 1

  rem -- Variables used --
  rem -- %a, %b, %c, %d, PARSE IP address.
  rem -- %p = current position from right.
  rem -- %s = substring at current position.
  rem -- %t = running total to get answer.
  rem -- %v = misc value.
  rem -- %x = current position from left, increment counter.
  rem --
  rem -- %%exp_number, %%exp_count, %%exp_answer. Example:
  rem -- 10 (%%exp_number = 10) to the power of 3 (%%exp_count = 3),
  rem -- which returns 1000 (%%exp_answer = 1000).
  rem --
  rem -- %%input = user entry.
  rem -- %%ipout = result of ip2bin conversion.

:ClearBUTTON
  dialog CLEAR, Input
  dialog CLEAR, Output
:EVLOOP
  dialog FOCUS, Input
  wait EVENT
  goto @event()

:Radio1CLICK
  goto EVLOOP

:OKBUTTON
  %%input = @dlgtext(Input)
  if %%input
    gosub @dlgtext(Radio1)
  end 
  goto EVLOOP

:CLOSE
  exit 
  stop 

  rem ------------------------ GOSUBS -------------------------

:ASCIIcode
  dialog SET, Output, @asc(%%input)"    spacebar=32  enter=13  esc=27"
  exit 

:ip2bin
  %x = 0
  rem -- Replace the "." with "|" for PARSE.
  repeat 
    %x = @succ(%x)
    if @equal(@substr(%%input, %x), ".")
      %%input = @strdel(%%input, %x)
      %%input = @strins(%%input, %x, "|")
    end 
  until @greater(%x, @len(%%input))
  parse "%a;%b;%c;%d", %%input
  if %a
    %%input = %a
    gosub dec2bin
    %%ipout = %t
  end 
  if %b
    %%input = %b
    gosub dec2bin
    %%ipout = %%ipout.%t
  end 
  if %c
    %%input = %c
    gosub dec2bin
    %%ipout = %%ipout.%t
  end 
  if %d
    %%input = %d
    gosub dec2bin
    %%ipout = %%ipout.%t
  end 
  dialog SET, Output, %%ipout
  exit 

:dec2hex
  if @numeric(%%input)
    dialog SET, Output, @hex(%%input)
  end 
  exit 

:hex2dec
  %%input = @upper(%%input)
  dialog SET, Input, %%input
  list CLEAR, 1
  %x = 0
  repeat 
    %x = @succ(%x)
    list ADD, 1, %x
  until @equal(%x, 9)
  list ADD, 1, "A | 10"
  list ADD, 1, "B | 11"
  list ADD, 1, "C | 12"
  list ADD, 1, "D | 13"
  list ADD, 1, "E | 14"
  list ADD, 1, "F | 15"
  %p = 0
  %s = ""
  %t = 0
  %v = 0
  %x = @len(%%input)
  %%exp_number = 16
  repeat 
    list SEEK, 1, 0
    %s = @substr(%%input, %x)
    if @numeric(%s)
      rem -- If numeric, it's OK like it is --
      %v = %s
    else 
      rem -- Look for it's numeric value in LIST 1 --
      if @match(1, %s)
        parse ";%v", @item(1)
      end 
    end 

    rem -- Get exponent --
    %%exp_count = %p
    gosub Get_Exp

    %v = @fmul(%v, %%exp_answer)
    %t = @fadd(%t,%v)
    %x = @pred(%x)
    %p = @succ(%p)
  until @equal(%x, 0)
  dialog SET, Output, %t
  list CLEAR, 1
  exit 

:dec2bin
  list CLEAR, 1
  rem -- Larger number than we should need, must be exponent of 2 --
  %v = 1099511627776
  repeat 
    rem -- Only load largest num we need and work down --
    if @greater(%%input, %v)@equal(%%input, %v)
      list ADD, 1, %v
    end 
    %v = @fdiv(%v, 2)
  until @equal(%v, 1)
  list ADD, 1, "1"
  %t = ""
  %v = 0
  %x = 0
  repeat 
    rem -- Check if divisible by num from list --
    if @greater(%%input, @item(1, %x))@equal(%%input, @item(1, %x))
      %%input = @mod(%%input, @item(1, %x))
      %t = %t"1"
    else 
      %t = %t"0"
    end 
    %x = @succ(%x)
  until @equal(%%input, 0)
  rem -- Add remaining placekeeping zeros --
  if @greater(@count(1), %x)
    repeat 
      %t = %t"0"
      %x = @succ(%x)
    until @equal(%x, @count(1))
  end 
  dialog SET, Output, %t
  list CLEAR, 1
  exit 

:bin2dec
  %p = 0
  %t = 0
  %v = 0
  %x = @len(%%input)
  %%exp_number = 2
  repeat 
    %v = @substr(%%input, %x)

    rem -- Get exponent --
    %%exp_count = %p
    gosub Get_Exp

    %v = @fmul(%v, %%exp_answer)
    %t = @fadd(%t,%v)
    %x = @pred(%x)
    %p = @succ(%p)
  until @equal(%x, 0)
  dialog SET, Output, %t
  exit 

  rem ====== THIS SUB (Get_Exp) IS USED BY SEVERAL ROUTINES ========
  rem -- Requires 2 vars be set and returns %%exp_answer. Example:
  rem -- 10 (%%exp_number = 10) to the power of 3 (%%exp_count = 3),
  rem -- which returns 1000 (%%exp_answer = 1000).

:Get_Exp
  %%exp_answer = %%exp_number
  if @greater(%%exp_count, 1)
    repeat 
      %%exp_answer = @fmul(%%exp_answer, %%exp_number)
      %%exp_count = @pred(%%exp_count)
    until @equal(%%exp_count, 1)
  else 
    if @equal(%%exp_count, 0)
      %%exp_answer = 1
    end 
  end 
  exit 
  rem ==============================================================

