Ascii alias

From Scriptwiki
Revision as of 14:22, 24 March 2007 by Vliedel (talk | contribs) (added input checks)

Jump to: navigation, search
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Written by Vliedel
; Contact: #help.script @ irc.QuakeNet.org
;
; This alias is usefull to quickly convert characters to ascii values.
;
; Usage: /ascii [[-rh] <input>]
;
; /ascii for a list of all characters.
; /ascii <text> to convert the text to the ascii values of all characters.
; /ascii -r <ascii values> to convert the ascii values to normal text.
; /ascii -h <text> to convert the text to the hexadecimal values of the characters.
; /ascii -rh <hexadecimal values> to convert the values to normal text.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

alias ascii {
  if ($1 == $null) {
    if ($window(@ascii)) { window -c @ascii }
    window -zk0 @ascii 20 20 150 650
    var %a = 1
    while (%a < 256) { aline @ascii $!chr( $+ %a $+ ) $chr(%a) | inc %a }
    return
  }
  var %l = $iif($left($1,1) == -,1) , %h = $iif(h isin $1,1) , %r = $iif(r isin $1,1)
  if (%l) && (%h) && (%r) {
    if (!$regex(%t,/^((^|\s)([\dA-F]?[\dA-F]))+$/i)) { echo -ag Usage: /ascii -rh <hexadecimal values> | return }
    var %i = $calc($0 - 1) , %t
    if (%i > $calc(900 - $len($2-))) { var %i = $v2 , %t = $chr(2) truncated }
    while (%i) { var %t = $chr($base($gettok($2-,%i,32),16,10,32)) $+ %t | dec %i }
    echo -ag $2- = %t
  }
  elseif (%l) && (%h) {
    var %i = $len($2-) , %t
    if ($calc(%i * 4) > 900) { var %i = $int($calc((900 - $len($2-))/3)) , %t = $chr(2) $+ truncated }
    while (%i) { var %t = $base($asc($mid($2-,%i,1)),10,16) %t | dec %i }
    echo -ag $2- $chr(15) $+ = %t
  }
  elseif (%l) && (%r) {
    if (!$regex($2-,/^((^|\s)(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9]))+$/)) { echo -ag Usage: /ascii -r <ascii values> | return }
    bset &asc 1 $2-
    var %m = $calc(900 - $len($2-)) + $0 -1)
    echo -ag $2- = $left($bvar(&asc,1-).text,%m) $iif(%m < $calc($0 -1),$chr(2) $+ truncated)
  }
  else {
    bset -t &asc 1 $1-
    var %m = $int($calc((900 - $len($1-))/4))
    echo -ag $1- $chr(15) $+ = $bvar(&asc,1,%m) $iif(%m < $bvar(&asc,0),$chr(2) $+ truncated)
  }
}