Bonjour,
Je ne vois pas comment faire un algo pour pouvoir faire un compteur type kilométrique avec de l'ascii ???
ex :
00000
00001
00002
...
00010
00011
00012
...
98342
98343
Et ainsi de suite.
Quelqu'un aurait une idée comment faire ?
Cdt,
Ju
Bonjour,
Je ne vois pas comment faire un algo pour pouvoir faire un compteur type kilométrique avec de l'ascii ???
ex :
00000
00001
00002
...
00010
00011
00012
...
98342
98343
Et ainsi de suite.
Quelqu'un aurait une idée comment faire ?
Cdt,
Ju
Bonjour
Pour ma part, je transformerais ma string en int avec un int.Parse,
un increment du int , puis un toString formaté de cet int
un truc du genre
cordialement
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 int a = 0; string cpt = a.ToString("000000"); int b = int.Parse(cpt); b++; cpt = b.ToString("000000");
Tu utilises une variable "int" et tu fais ToString().LPad('0', 8)...quelque chose dans le genre (je n'ai pas vérifier l'exactitude de mon bout de code)
Jusque là pour le int je suis d'accord mais je souhaiterais passer aussi des lettres.
Par exemple :
aaa + 1 = aab ...etc
slc + 2 = sle ...
bonjour
voici une classe qui converti un string en base (2 to 36)
donc meme chose qu'avant , sauf que tu convertit ton string de 36 a 10, puis le int.parse, increment du int puis int en string en conversion du 10 en 36
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 /// <summary> /// Class BaseConverter to pass from a data representation to another from base 2 to 36 /// </summary> public class BaseConverter { /// <summary> /// Function Convert to Convert number in string representation from base:from to base:to /// </summary> /// <param name="from">source base of the number to convert (2 to 36)</param> /// <param name="to">Destination base for the number to convert (2 to 36)</param> /// <param name="s">Number to convert in a string format</param> /// <returns>A string of the converted number in base "to"</returns> public static String Convert(int from, int to, String s) { //Return error if input is empty if (String.IsNullOrEmpty(s)) { return ("Error: Nothing in Input String"); } //only allow uppercase input characters in string s = s.ToUpper(); //only do base 2 to base 36 (digit represented by charecaters 0-Z)" if (from < 2 || from > 36 || to < 2 || to > 36) { return ("Base requested outside range"); } //convert string to an array of integer digits representing number in base:from int il = s.Length; int[] fs = new int[il]; int k = 0; for (int i = s.Length - 1; i >= 0; i--) { if (s[i] >= '0' && s[i] <= '9') { fs[k++] = (int)(s[i] - '0'); } else { if (s[i] >= 'A' && s[i] <= 'Z') { fs[k++] = 10 + (int)(s[i] - 'A'); } else { return ("Error: Input string must only contain any of 0-9 or A-Z"); } //only allow 0-9 A-Z characters } } //check the input for digits that exceed the allowable for base:from foreach (int i in fs) { if (i >= from) { return ("Error: Not a valid number for this input base"); } } //find how many digits the output needs int ol = il * (from / to + 1); int[] ts = new int[ol + 10]; //assign accumulation array int[] cums = new int[ol + 10]; //assign the result array ts[0] = 1; //initialise array with number 1 //evaluate the output for (int i = 0; i < il; i++) //for each input digit { for (int j = 0; j < ol; j++) //add the input digit times (base:to from^i) to the output cumulator { cums[j] += ts[j] * fs[i]; int temp = cums[j]; int rem = 0; int ip = j; do // fix up any remainders in base:to { rem = temp / to; cums[ip] = temp - rem * to; ip++; cums[ip] += rem; temp = cums[ip]; } while (temp >= to); } //calculate the next power from^i) in base:to format for (int j = 0; j < ol; j++) { ts[j] = ts[j] * from; } for (int j = 0; j < ol; j++) //check for any remainders { int temp = ts[j]; int rem = 0; int ip = j; do //fix up any remainders { rem = temp / to; ts[ip] = temp - rem * to; ip++; ts[ip] += rem; temp = ts[ip]; } while (temp >= to); } } //convert the output to string format (digits 0,to-1 converted to 0-Z characters) String sout = String.Empty; //initialise output string bool first = false; //leading zero flag for (int i = ol; i >= 0; i--) { if (cums[i] != 0) { first = true; } if (!first) { continue; } if (cums[i] < 10) { sout += (char)(cums[i] + '0'); } else { sout += (char)(cums[i] + 'A' - 10); } } if (String.IsNullOrEmpty(sout)) { return "0"; } //input was zero, return 0 //return the converted string return sout; } }
Comme c'est compliqué ...
Voici ma méthode
Il suffit simplement de respecter la décompositions des nombres.
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 static void Main() { int test = 20; string str_test = IntToString("0123456789abcdef", test); // ici le résultat est "14", c'est de l'hexa } private static string IntToString(string strSymboles, int test) { if (strSymboles == null || strSymboles.Length == 0) return string.Empty; char[] symboles = strSymboles.ToCharArray(); int rest = test; string result = string.Empty; while (rest > 0) { int index = rest % symboles.Length; result = symboles[index] + result; rest = rest / symboles.Length; } return result; }
Avec cette méthode tu peux incrémenter n'importe quoi dès l'instant où tu définit toi même l'alphabet.
Exemple :
Pourquoi ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 int test = 20; string str_test = IntToString("a5dà4é)[%;,c", test); // = "5%"
L'alphabet utilisé fait 12 caractères, donc on est en base 12.
La position des caractères utilisé (exprimé en indexe 0) est :
'5' = position 1
'%' = position 8
Résultat :
'5' * 12 + '%' * 1 = 1 * 12 + 8 = 20
oula ... je vois a quoi sert ton bout de code @ctxnop mais on va dire que ce n'est pas ça que je cherche a faire ...
imaginons que je rentre une longueur de chaine ex : 3
je souhaite faire défilé par exemple les 3 caractères 'a', 'b', 'c'
donc je voudrais que ma méthode m'affiche :
aaa
aab
aac
aba
abb
abc
aca
acb
acc
baa
bab
bac
bba
bbb
bbc
bca
bcb
bcc
... et ainsi de suite jusqu'à ccc ... afin d'obtenir toutes les combinaisons possibles des lettres ....
Partager