RJosh commented on a Page, Binary to ASCII  -  Dec 19, 2009

Just a suggestion, the use of global variables should be used. Locals would have sufficed.

Also for longtintoascii, in the if instead of needing to use calc just use

if (8 \\ $len($1)) { echo -sa * /longbintoascii SyntaxError: Invalid Input | return }

aswell as for your binary2ascii all those if statements could be avoided using a simple loop.

var %a = 1,%b
while ($mid($1,%a,1) != $null) {
  inc %b $calc( $v1 * 2 ^ ( 8 - %a ) )
  inc %a
}
var %t = $+(%t,$iif($chr(%b) == $chr(32),$str($chr(1),3),$v1)
var %b

If you don't like the multiple chr(1)'s in place of spaces you can do it another way that's suitable for you, i simply chose that cause well, it's very unlikely that there will be 3 chr(1)'s consecutive in a string being processed.

Same goes for your ascii2binary

var %a = 1,%b = 1
while ($asc($mid($1-,%a,1)) != $null) {
  var %x = $v1
  while (%x > 0) {
    var %y = $iif(2 \\ $v1,1,0) $+ %y,%x = $floor($calc(%x / 2))
  }
  var %bin = $+(%bin,$str(0,$calc(8 - $len(%y))),%y)
  inc %a
}

Here are my versions of your code:

/*
*   Binary -> Decimal
*/
todec {
  if (8 // $len($$1-)) {
    noop $regex(dec,$$1-,/([01]{8})/g)
    var %a = 1,%b,%t
    while ($regml(dec,%a)) {
      var %c = 1,%d = $v1
      while ($mid(%d,%c,1) != $null) {
        inc %b $calc( $v1 * 2 ^ ( 8 - %c ) )
        inc %c
      }
      var %t = $+(%t,$iif($chr(%b) != $chr(32),$v1,$str($chr(1),3)))
      var %b
      inc %a
    }
    return $replace(%t,$str($chr(1),3),$chr(32))
  }
  return
}
/*
*   Decimal -> Binary
*/
binary {
  var %a = $len($1-)
  while (%a) {
    var %b = $tobin($asc($mid($1-,%a,1))) $+ %b    
    dec %a
  }
  return %b
}
tobin {
  var %x = $1-
  while (%x > 0) {
    var %y = $iif(2 \\ %x,1,0) $+ %y
    var %x = $floor($calc(%x / 2))
  }
  return $+($str(0,$calc(8- $len(%y))),%y)
}
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.