int number = 11; System.out.println("Int format: " + String.format("%08d", number)); System.out.println("Hex format: " + String.format("%08x", number)); int iBinary = Integer.parseInt(Integer.toBinaryString(number)); System.out.println("Bin format: " + String.format("%08d", iBinary)); /* * Int format: 00000011 * Hex format: 0000000b * Bin format: 00001011 */
Quelques mots de remerciements seront grandement appréciés. Ajouter un commentaire
119 internautes nous ont dit merci ce mois-ci
String str = "0101"; int strLg = str.length(); String zerosAgauche = ""; for(int i = 7; i >-1; i--) zerosAgauche+= (strLg>i?"":"0"); zerosAgauche+= str; System.out.println("Avec ajout de " + (8 -strLg) + " zero" + ((8 -strLg)>1?"s":"") + " à gauche : " + zerosAgauche);
String str = "11"; // 6 zeros a gauche // str = "10101010"; // 0 //str = ""; // 8 int strLg = str.length(); System.out.println("Sans zéro(s) à gauche : " + str); String zerosAgauche = (strLg>7?"":"0") + (strLg>6?"":"0") + (strLg>5?"":"0") + (strLg>4?"":"0") + (strLg>3?"":"0") + (strLg>2?"":"0") + (strLg>1?"":"0") + (strLg>0?"":"0") + str; System.out.println("Avec zéro(s) à gauche : " + zerosAgauche);
String [] norma( String [] a ){ String [] b = new String[ a.length ]; for (int i = 0; i < a.length; i++) { int l = a[i].length(); for ( int j = 0; j < 8-l; j++) { A[i]="0"+A[i]; } b[i] = a[i]; } return b; }
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.