Conversion binaire/decimale/hexadecimale en p
genzo03
Messages postés
22
Statut
Membre
-
jjikojio -
jjikojio -
Bonjour, je souhaiterais développer un programme en php où on demande à l'utilisateur de saisir un nombre dont il choisira le type(binaire/decimal/hexa) et de choisir dans quel type il veut le convertir(avec les boutons radio)
Merci de votre réponse
Merci de votre réponse
Configuration: Windows XP Internet Explorer 7.0
1 réponse
-
salut,
si ca t'interesse toujours (depuis le temps ^^), j'ai developpé un prog qui fait ce que tu demandes (et plus) :<html> <head> <title>Numeration base</title> </head> <body> <center> <h1>Changing a number from a base to another one</h1> <form name="form1" method="POST" action="base2.php"> <?php $num = @$_POST['num']; $base1 = @$_POST['base1']; $base2 = @$_POST['base2']; ?> <table> <tr> <td>Number to change :</td> <td><input type=text name="num" size=6 value="<?php echo $num ?>"></td> </tr> <tr> <td>From the base :</td> <td><input type=text name="base1" size=3 value="<?php echo $base1 ?>"></td> </tr> <tr> <td>To the base :</td> <td><input type=text name="base2" size=3 value="<?php echo $base2 ?>"></td> </tr> </table> <input type=submit name="valider" value="Result"> </form> <?php function test() { global $num, $base1, $base2; $tab = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F'); if (empty($num)) { echo "You didn't give a number to transform !<br>"; } if (empty($base1)) { echo "You didn't give the initial base. I suppose it is 10 !<br>"; } if (empty($base2)) { echo "The target base is missing !<br>"; } if (!empty($num) && !empty($base2)) { if (empty($base1)) { $base1 = "10"; } if ($base1 < 2 || $base1 > 16) { echo "The initial base $base1 is not treated here !<br>"; return; } if ($base2 < 2 || $base2 > 16) { echo "The target base $base2 is not treated here !<br>"; return; } $tmp = array_slice($tab, 0, $base1 - 1); $num = strtoupper($num); for ($i = 0; $i < strlen($num); $i++) { if (!in_array($num[$i], $tmp)) { echo "The number $num is not in base $base1 !<br>"; return; } } $res = base_convert($num, $base1, $base2); echo "<h1>$res</h1><br>"; } } test(); ?> </center> </body> </html>
à+