This is three snippets to convert binary to ascii and ascii to binary
Syntax
/echo -a $binary2ascii(01001101) [== M]
/longbintoascii 0100110101100001011010110110010100100000011011010110010100100000011000100110100101101110011000010111001001111001
/asciitobinary Make me binary
binary2ascii {
var %a = 0
while (%a <= $len($1)) {
if ($mid($1,%a,1) isnum 2-9) { echo -a $1- is not a binary number, FAIL | halt }
inc %a
}
set %re 0
if ($left($1,1) == 1) { set %re $calc(%re + 128) }
if ($mid($1,2,1) == 1) { set %re $calc(%re + 64) }
if ($mid($1,3,1) == 1) { set %re $calc(%re + 32) }
if ($mid($1,4,1) == 1) { set %re $calc(%re + 16) }
if ($mid($1,5,1) == 1) { set %re $calc(%re + 8) }
if ($mid($1,6,1) == 1) { set %re $calc(%re + 4) }
if ($mid($1,7,1) == 1) { set %re $calc(%re + 2) }
if ($right($1,1) == 1) { set %re $calc(%re + 1) }
return %re
unset %re
}
longbintoascii {
if ($calc($len($1) % 8) != 0) { /echo -a Invalid Binary format, Convert by BYTE not BIT | halt }
var %5 = 0, %2 = $calc($len($1) / 8)
while (%5 < $len($1)) {
set %rdedse $+(",$left($right(%rdedse,-1),-1),$chr($binary2ascii($mid($1,$calc(%5 + 1),8))),")
inc %5 8
}
echo -a $left($right(%rdedse,-1),-1)
unset %rdedse
}
asciitobinary {
var %t = 1
while (%t <= $len($1-)) {
var %y = $asc($mid($1-,%t,1))
var %q = $iif($calc(%y - 128) > -1,1,0)
var %y = $iif($calc(%y - 128) > -1,$calc(%y - 128),%y)
var %q = $iif($calc(%y - 64) > -1,$+(%q,1),$+(%q,0))
var %y = $iif($calc(%y - 64) > -1,$calc(%y - 64),%y)
var %q = $iif($calc(%y - 32) > -1,$+(%q,1),$+(%q,0))
var %y = $iif($calc(%y - 32) > -1,$calc(%y - 32),%y)
var %q = $iif($calc(%y - 16) > -1,$+(%q,1),$+(%q,0))
var %y = $iif($calc(%y - 16) > -1,$calc(%y - 16),%y)
var %q = $iif($calc(%y - 8) > -1,$+(%q,1),$+(%q,0))
var %y = $iif($calc(%y - 8) > -1,$calc(%y - 8),%y)
var %q = $iif($calc(%y - 4) > -1,$+(%q,1),$+(%q,0))
var %y = $iif($calc(%y - 4) > -1,$calc(%y - 4),%y)
var %q = $iif($calc(%y - 2) > -1,$+(%q,1),$+(%q,0))
var %y = $iif($calc(%y - 2) > -1,$calc(%y - 2),%y)
var %q = $iif($calc(%y - 1) > -1,$+(%q,1),$+(%q,0))
var %y = $iif($calc(%y - 1) > -1,$calc(%y - 1),%y)
set %asciitobinout $+(%asciitobinout,%q)
inc %t
}
echo -a %asciitobinout
unset %asciitobinout
}
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)
}