09/08/2008, 22:58
Voici une collection de procédures qui vous aideront à convertir des nombres entre diverses bases numériques.
La syntaxe de chaque procédure est la même : vous devez passer le nombre à convertir en paramètre.
décimal -> binaire
décimal -> hexadécimal
décimal -> octal
binaire -> décimal
binaire -> hexadécimal
binaire -> octal
hexadécimal -> décimal
hexadécimal -> binaire
hexadécimal -> octal
octal -> binaire
octal -> décimal
octal -> hexadécimal
La syntaxe de chaque procédure est la même : vous devez passer le nombre à convertir en paramètre.
décimal -> binaire
tcl
proc dec2bin dec2bin_dec {
binary scan [binary format I $dec2bin_dec] B32 dec2bin_bin
return $dec2bin_bin
}
décimal -> hexadécimal
tcl
proc dec2hex dec2hex_dec {
return [format %x $dec2hex_dec]
}
décimal -> octal
tcl
proc dec2oct dec2oct_dec {
return [format %o $dec2oct_dec]
}
binaire -> décimal
tcl
proc bin2dec bin2dec_bin {
return [hex2dec [bin2hex $bin2dec_bin]]
}
binaire -> hexadécimal
tcl
proc bin2hex bin2hex_binary {
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 bin2hex_diff [expr {4-[string length $bin2hex_binary]%4}]
if {$bin2hex_diff != 4} {
set bin2hex_binary [format %0${bin2hex_diff}d$bin2hex_binary 0]
}
regsub -all .... $bin2hex_binary {$bin2hex_array(&)} bin2hex_hexa
return [subst $bin2hex_hexa]
}
binaire -> octal
tcl
proc bin2oct bin2oct_bin {
set bin2oct_dec [hex2dec [bin2hex $bin2oct_bin]]
return [format %o $bin2oct_dec]
}
hexadécimal -> décimal
tcl
proc hex2dec hex2dec_hexa {
return [expr 0x$hex2dec_hexa]
}
hexadécimal -> binaire
tcl
proc hex2bin hex2bin_hexa {
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]} $hex2bin_hexa {} hex2bin_hexa
regsub -all . $hex2bin_hexa {$hex2bin_array(&)} hex2bin_binary
return [subst $hex2bin_binary]
}
hexadécimal -> octal
tcl
proc hex2oct hex2oct_hex {
return [dec2oct [hex2dec $hex2oct_hex]]
}
octal -> binaire
tcl
proc oct2bin oct2bin_oct {
return [dec2bin [oct2dec $oct2bin_oct]]
}
octal -> décimal
tcl
proc oct2dec oct2dec_oct {
set oct2dec_oct 0$oct2dec_oct
return [format %i $oct2dec_oct]
}
octal -> hexadécimal
tcl
proc oct2hex oct2hex_oct {
return [dec2hex [oct2dec $oct2hex_oct]]
}
Toute l'actualité de mes scripts ici (dernière mise à jour le 14/07/2018)
Tout programme comporte au moins un bug et pourrait être raccourci d'au moins une instruction, de quoi l'on peut déduire que tout programme peut être réduit à une seule instruction qui ne fonctionne pas.
Tout programme comporte au moins un bug et pourrait être raccourci d'au moins une instruction, de quoi l'on peut déduire que tout programme peut être réduit à une seule instruction qui ne fonctionne pas.

