tcl
# décimal -> binaire
proc dec2bin value {
binary scan [binary format I $value] B32 output
return $output
}
# décimal -> hexadécimal
proc dec2hex value {
return [format %x $value]
}
# décimal -> octal
proc dec2oct value {
return [format %o $value]
}
# binaire -> décimal
proc bin2dec value {
return [hex2dec [bin2hex $value]]
}
# binaire -> hexadécimal
proc bin2hex value {
array set bin2hex_array {
0000 0 0001 1 0010 2 0011 3 0100 4
0101 5 0110 6 0111 7 1000 8 1001 9
1010 a 1011 b 1100 c 1101 d 1110 e 1111 f
}
set diff [expr {4-[string length $value]%4}]
if { $diff != 4 } {
set value [format %0${diff}d$value 0]
}
regsub -all .... $value {$bin2hex_array(&)} hex_value
return [subst $hex_value]
}
# binaire -> octal
proc bin2oct value {
set dec_value [hex2dec [bin2hex $value]]
return [format %o $dec_value]
}
# hexadécimal -> décimal
proc hex2dec value {
return [expr 0x$value]
}
# hexadécimal -> binaire
proc hex2bin value {
array set hex2bin_array {
0 0000 1 0001 2 0010 3 0011 4 0100
5 0101 6 0110 7 0111 8 1000 9 1001
a 1010 b 1011 c 1100 d 1101 e 1110 f 1111
A 1010 B 1011 C 1100 D 1101 E 1110 F 1111
}
regsub {^0[xX]} $value {} value
regsub -all . $value {$hex2bin_array(&)} bin_value
return [subst $bin_value]
}
# hexadécimal -> octal
proc hex2oct value {
return [dec2oct [hex2dec $value]]
}
# octal -> binaire
proc oct2bin value {
return [dec2bin [oct2dec $value]]
}
# octal -> décimal
proc oct2dec value {
set value 0$value
return [format %i $value]
}
# octal -> hexadécimal
proc oct2hex value {
return [dec2hex [oct2dec $value]]
}
Source : http://www.boulets.oqp.me/tcl/routines/t...-0002.html