Bonjour,

J'aimerai avoir un avis extérieur sur mon code, sur l'algo en lui même.
Soyer indulgent.

J'ai besoin de convertir mes nombres en base 26.
Comme pour les colonnes d'Excel/
Je me limite à "A" à "ZZ" soit 702.

Je n'utilise pas le 0

J'ai l'impression que je n'ai pas choisit la meilleur voie

Cordialement

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public class Grille {
	private static String table26 = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
	public static char nombreVersTable26(int nombre) {
		//le nombre doit être supérieur ou égale à 1 et inférieur ou égale à 26 
		return table26.charAt(nombre);
	}
 
	public static String numeroVersTable26(final int numeroToConvert) {
		int numero = numeroToConvert;
 
		if(numeroToConvert < 1 || numeroToConvert > 702) {
			throw new IllegalArgumentException("Le nombre à convertir doit être compris entre 1 et 702");
		}
 
		if(numero < 27) {
return "" + nombreVersTable26(numero%27);			
		}
 
		String numeroTable26 = "";
 
do {
	int reste = numero % 26;
			numero = numero / 26;
 
			if(reste == 0) {
		numero = numero -1;
		numeroTable26 = nombreVersTable26(numero) + numeroTable26;
 
				reste = 26;
				numeroTable26 += nombreVersTable26(reste);
 
				numero = 0;
			} else {
			numeroTable26 = nombreVersTable26(reste) + numeroTable26;
			}
 
		} while(numero > 0);
		return numeroTable26;
		}
 
	public static int indexOfChar(char c) {
		return table26.indexOf(c);
	}
 
	public static double table26VersNombre(final String chaineToConvert) {
		String chaine = new StringBuilder(chaineToConvert.toUpperCase()).reverse().toString();
double		nombre = 0;
 
		for(int i=0;i<chaine.length();i++) {
		nombre += Math.pow(26, i) * indexOfChar(chaine.charAt(i));
			System.out.println("indexe" + indexOfChar(chaine.charAt(i)));
	}
	return nombre;
	}
 
	public static void main(String [] args) {
//for(int i=26;i <10000;i = i + 26) {
	for(int i=1;i <10000;i++) {
	String numero =numeroVersTable26(i);
	double resultat = table26VersNombre(numero);
 
		System.out.println(
				i +" " +
		numero + " " + 
						resultat
		);
 
		if(i !=resultat ) {
			System.out.println(
"******");	
		}
		}
}
}