Tcl language Note: TCL Practice II

Source: Internet
Author: User

First, practice

1. Binary Turn Decimal

proc b2d {b} {    set0;  Set len [string  length $b]    for {set0} {$i <$len} { incr i} {        int([string index $b $i]*pow (2, $len-$i-1)]    }     return $sum}

2. Decimal Turn binary

proc D2B {d} {    set""     while {$d! =0}        {set " [expr $d%2] $b "        set d [Expr $d/2]    }    return  $b}

3. Convert from length to subnet mask

proc Len2mask {len} {SetMask""     for{SetI0} {$i <4} {incr i} {if{$len >=8} {lappend mask [b2d [FORMATB8 8] ] incr len-8        } Else{Lappend Mask [b2d [FORMATB $len8]]            SetLen0        }    }    return[Join $mask.]}

4. Convert to length based on subnet mask

 proc Mask2len {mask} { set   MSKs [Split $mask.]  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}  

Tcl language Note: TCL Practice II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.