## C O N F I G :
#should the script work on all channels? 1:on, 0:off
set calc(all) 0
#if $calc(all) is off, what channels should the script work on?
set calc(chans) "#mychan #yourchan #ourchan"
## S C R I P T :
#binds
bind pub - ".bin2dec" bin2dec
bind pub - ".binary2decimal" bin2dec
bind pub - ".decimal" bin2dec
bind pub - ".dec2bin" dec2bin
bind pub - ".decimal2binary" dec2bin
bind pub - ".binary" dec2bin
#redefining some vars
set calc(chans) [string tolower $calc(chans)]
##procs
#dec2bin
proc dec2bin {nick host hand chan args} {
#load in some global vars
global calc
#set some vars
set chan [string tolower $chan]
set temp $args
set length 0
set answer ""
#check if the chan is valid
if {!$calc(all) && [lsearch -exact $calc(chans) $chan] == -1} {return 0}
#check if the string contains only numbers
if {![regexp ^(\[0-9\])+$ $args]} {
putserv "notice $nick :You did not specify a valid number!"
return 0
}
#check if the number is not higher than 16383, since the calc cant evaluate higher numbers
if {[expr $args - 16383] > 0} {
putserv "notice $nick :Your number is higher than 16383, and therefore I can not calculate a correct answer.."
return 0
} elseif {$args <= 0} {
putserv "notice $nick :Your number is zero or negative. I can not calculate such numbers into binary.."
return 0
}
#convert the decimal number to binary
for {set f 13} {$f > 0} {incr f -1} {
if {[expr $args - pow(2,$f)] > -1} {
lappend answer 1
set args [expr $args - pow(2,$f)]
} else {
lappend answer 0
}
}
if {$args > 0} {
lappend answer 1
} else {
lappend answer 0
}
#set some more vars
set answer [join $answer ""]
set ans ""
set foundValue 0
#remove dud 0's at the start of the string
foreach char [split $answer ""] {
if {($char == 0) && ($foundValue == 0)} {
continue
} else {
lappend ans $char
set foundValue 1
}
}
#display the result to the chan
puthelp "privmsg $chan :Binary calculation of $temp, resolved to [join $ans ""]"
} ;#end proc
#bin2dec
proc bin2dec {nick host hand chan args} {
#load in some global vars
global calc
#set some vars
set chan [string tolower $chan]
set temp $args
set answer 0
#check if the chan is valid
if {!$calc(all) && [lsearch -exact $calc(chans) $chan] == -1} {return 0}
#check if the string contains only numbers
if {![regexp ^(\[0-1\])+$ $args]} {
putserv "notice $nick :You did not specify a valid binary number!"
return 0
}
#check if the length of the binary number is longer than 14
if {[string length $args] > 14} {
putserv "notice $nick :The binary value you specified was too high for me to calculate.."
return 0
}
#convert the binary into decimal
for {set i 0} {$i < 14} {incr i} {
if {[expr [string length $args] - 1] < $i} {break}
set length [expr [string length $args] - 1 - $i]
set char [string index $args $length]
set blah [expr $char * pow(2,$i)]
set answer [expr $answer + $blah]
}
puthelp "privmsg $chan :Decimal calculation of $temp, resolved to [expr int($answer)]"
} ;#end proc