TCL语言笔记:TCL练习二
一、练习
1.二进制转十进制
proc b2d {b} { set sum 0;set len [string length $b] for {set i 0} {$i<$len} {incr i} { incr sum [expr int([string index $b $i]*pow(2,$len-$i-1))] } return $sum }
2.十进制转二进制
proc d2b {d} { set b "" while {$d!=0} { set b "[expr $d%2]$b" set d [expr $d/2] } return $b }
3.根据长度转换成子网掩码
proc len2mask {len} { set mask "" for {set i 0} {$i<4} {incr i} { if {$len>=8} { lappend mask [b2d [formatb 8 8]] incr len -8 } else { lappend mask [b2d [formatb $len 8]] set len 0 } } return [join $mask .] }
4.根据子网掩码转换成长度
proc mask2len {mask} { set msks [split $mask .] set len 0 for {set i 0} {$i<4} {incr i} { set m [lindex $msks $i] set str [d2b $m] incr len [string length [string trim $str 0]] } return $len }
本文来自博客园,作者:HuTiger,转载请注明原文链接:https://www.cnblogs.com/huguodong/p/5902897.html,欢迎加入qq群927460240学习讨论