So that we update the Tcl cookbook :)
proc thermal {dec {len 20}} {
return [format %0${len}b [expr {(1 << $dec) - 1}]]
}
#_______________________
proc invertSubst {str idx} {
return [string replace $str 0 end-$idx][string range $str 0 end-$idx]
}
#_______________________
proc invertStr {str} {
return [join [lreverse [split $str {}]] {}]
}
#_______________________
proc to_thermal {num {len 10} {filler 0}} {
return "[string repeat $filler [expr {$len - $num}]][string repeat 1 $num]"
}
#_______________________
# testing
set ckl 100000
foreach d {0 1 8 10 19 20 30} {
set time0 [time {set t [thermal $d]} $ckl]
set timet [time {set t2 [to_thermal $d]} $ckl]
set time1 [time {set t1 [invertSubst $t $d]} $ckl]
set time2 [time {set t2 [invertStr $t]} $ckl]
puts "decimal: $d"
puts "therm 1: $t ($time0)"
puts "therm 2: $t2 ($timet)"