-
Notifications
You must be signed in to change notification settings - Fork 4
/
TCL script to convert Binary to Hexadecimal.tcl
68 lines (67 loc) · 1.83 KB
/
TCL script to convert Binary to Hexadecimal.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#Written by : Udaya Shankar S
#dated: 11.11.2022
#TCL script to convert Binary to Hexadecimal
#Version 1.0
set a "11110101100101111"
set l [string length $a]
set b [split $a ""]
if {[expr $l%4] == 0} {
set a $a
} elseif {[expr $l%4] == 1} {
set c [concat "000" "$b" "\b"]
set a [regsub -all " " $c ""]
#puts $c;puts $a ;
} elseif {[expr $l%4] == 2} {
set c [concat "00" "$b" "\b"]
set a [regsub -all " " $c ""]
#puts $c;puts $a ;
} elseif {[expr $l%4] == 3} {
set c [concat "0" "$b" "\b"]
set a [regsub -all " " $c ""]
#puts $c;puts $a ;
} else {
continue
}
set in [string range $a 0 end]
set inl [string length $in]
set h [list]
for { set i 0 } { $i < $inl } {incr i 4} {
set x [string range $in $i [expr $i+3 ] ]
if {[string equal $x "0000"]} {
set t "0"
} elseif {[string equal $x "0001"]} {
set t "1"
} elseif {[string equal $x "0010" ] } {
set t "2"
} elseif {[string equal $x "0011" ]} {
set t "3"
} elseif {[string equal $x "0100" ]} {
set t "4"
} elseif {[string equal $x "0101" ] } {
set t "5"
} elseif {[string equal $x "0110" ] } {
set t "6"
} elseif {[string equal $x "0111" ] } {
set t "7"
} elseif {[string equal $x "1000" ] } {
set t "8"
} elseif {[string equal $x "1001" ] } {
set t "9"
} elseif { [string equal $x "1010" ]} {
set t "A"
} elseif {[string equal $x "1011" ] } {
set t "B"
} elseif { [string equal $x "1100" ] } {
set t "C"
} elseif {[string equal $x "1101" ]} {
set t "D"
} elseif {[string equal $x "1110" ] } {
set t "E"
} elseif {[string equal $x "1111" ] } {
set t "F"
} else {
continue
}
set h [append h "$t" ""]
}
puts " Hexedecimal Equivalent of $a: [regsub -all " " $h ""]"