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 */
private String completer(String n){ StringBuilder sb = new StringBuilder(); for(int i=0; i<10-n.length; i++){ sb.append("0"); } sb.append(n); return sb.toString(); }
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);
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionString 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);}
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; }