j'ai besoin que l'on m'explique ce que cette ligne de code fait et de me donner l'equivalent en string (j'ai un element de type string string word).Code:unsigned char *str = (unsigned char *)in;
merci
Version imprimable
j'ai besoin que l'on m'explique ce que cette ligne de code fait et de me donner l'equivalent en string (j'ai un element de type string string word).Code:unsigned char *str = (unsigned char *)in;
merci
Déclare une variable str de type unsigned char et l'initialise avec l'adresse de la variable in castée en unsigned char, parce que à priori in n'est pas de type unsigned char
in pourrait être par exemple de type string et cette déclaration te donnerait un pointeur, non pas sur la chaine, mais sur chacun (le premier plus exactement) des caractères la composant.
quant à l'équivalent en strin je passe la main à plus compétent :D
Bonjour,
Elle fait un cast à la C de la variable in vers unsigned char*. Quel est le résultat effective ? Cela dépend beaucoup du type de la variable in. Pourrais-tu le préciser ?
je veux la changer en string comme ceci :Code:
1
2
3 void get_hashes(unsigned int hash[], char *in) { unsigned char *str = (unsigned char *)in;
et changer le ligneCode:
1
2 void get_hashes(unsigned int hash[], string in) {
par son equivalent en stringCode:unsigned char *str = (unsigned char *)in;
Quelque chose comme ceci ?Code:unsigned char *str = reinterpret_cast<unsigned char *>(in.c_str());
Si tu veux qu'on t'aide, il faudra être plus complet et précis...
cet ligne de code fais parties d'un filtre de bloom il utilise des char mais moi je doit manipuler des element de la biblitheque string alors voici le code de la fonction
moi ce que je veux faire c'est saCode:
1
2
3
4
5
6
7
8
9
10
11
12 void get_hashes(unsigned int hash[], char *in) { unsigned char *str = (unsigned char *)in; int pos = strlen(in); hash[0] = RSHash (str, pos); hash[1] = DJBHash (str, pos); hash[2] = FNVHash (str, pos); hash[3] = JSHash (str, pos); hash[4] = PJWHash (str, pos); hash[5] = SDBMHash(str, pos); hash[6] = DEKHash (str, pos); }
Code:
1
2
3
4
5
6
7
8
9
10
11
12 void get_hashes(unsigned int hash[], String *in) { unsigned char *str = (unsigned char *)in; // changer cel la par son equivalent en string int pos = strlen(in); // et convertir un string en entier hash[0] = RSHash (str, pos); hash[1] = DJBHash (str, pos); hash[2] = FNVHash (str, pos); hash[3] = JSHash (str, pos); hash[4] = PJWHash (str, pos); hash[5] = SDBMHash(str, pos); hash[6] = DEKHash (str, pos); }
Code:
1
2 unsigned char *str = (unsigned char *)&*in.begin(); int pos = in.length();}
Avec des fonctions de hachage déclarées correctement (lire: const), on aurait ceci:
Et si les fonctions sont mal déclarées, utiliser explicitement un const_cast au besoin.Code:
1
2
3
4
5
6
7
8
9
10
11
12 void get_hashes(unsigned int hash[], std::string const &in) { unsigned char const *str = reinterpret_cast<unsigned char const*>(in.c_str()); size_t pos = in.length(); hash[0] = RSHash (str, pos); hash[1] = DJBHash (str, pos); hash[2] = FNVHash (str, pos); hash[3] = JSHash (str, pos); hash[4] = PJWHash (str, pos); hash[5] = SDBMHash(str, pos); hash[6] = DEKHash (str, pos); }
Ne JAMAIS cacher un const_cast derrière un cast C-style, c'est la porte ouverte aux erreurs!
il ma afficher une erreur qui est
error: invalid conversion from ‘const unsigned char*’ to ‘unsigned char*’
Donc, c'est que les fonctions de hash sont mal déclarées.
- Si tu as accès aux sources et déclarations de ces fonctions, corrige.
- Sinon, utilise des const_cast.
ba si vous voulez je peus vous donner tou le code
Ça peut être intéressant.
voici tout le code a vous de m'aider et merciii
Code:
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245 #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <stdarg.h> using namespace std; #define FILTER_SIZE 10 #define NUM_HASHES 7 #define WORD_BUF_SIZE 32 #define FILTER_SIZE_BYTES (1 << (FILTER_SIZE - 3)) #define FILTER_BITMASK ((1 << FILTER_SIZE) - 1) /* function de hashage */ unsigned int RSHash (unsigned char *, unsigned int); unsigned int DJBHash (unsigned char *, unsigned int); unsigned int FNVHash (unsigned char *, unsigned int); unsigned int JSHash (unsigned char *, unsigned int); unsigned int PJWHash (unsigned char *, unsigned int); unsigned int SDBMHash(unsigned char *, unsigned int); unsigned int DEKHash (unsigned char *, unsigned int); /* fonction de help */ void Verifir_Sys(unsigned char[], char *file,char *file1); void insert_word(unsigned char[], string word); int in_dict(unsigned char[], string word); void get_hashes(unsigned int[], string word); int main(int argc, char *argv[]) { unsigned char filter[FILTER_SIZE_BYTES]; if(argc < 3){ cout<<"le nombre de parametre a fournir au fonction est insuffisant"<<endl; } else Verifir_Sys(filter,argv[1],argv[2]); return 1; } void Verifir_Sys(unsigned char filter[], char *filename,char *filename1) { string AdrePortSour; string AdrePortDest; int test; fstream fichier(filename); fstream fichiersysAck(filename1); if ( !fichier ) { // si le fichier n'est pas trouv� cout << "fichier inexistant"; } else { // sinon bool continuer = true; // indicateur de fin de fichier while( continuer ) { string ch; // chaine contenant une ligne du fichier fichier >> ch; // ranger une ligne dans ch if ( ch != "" ){ if(ch=="S"){ //si la ligne n'est pas vide faire fichier >> ch; AdrePortSour=AdrePortSour+ch; fichier >> ch; AdrePortSour=AdrePortSour+" "+ch; cout<<AdrePortSour<<endl; AdrePortSour=""; insert_word(filter,AdrePortSour); } if(ch=="SA"){ //si la ligne n'est pas vide faire fichier >> ch; fichier >> ch; fichier >> ch; fichier >> ch; AdrePortDest=AdrePortDest+ch; fichier >> ch; AdrePortDest=AdrePortDest+" "+ch; cout<<AdrePortDest<<endl; AdrePortDest=""; test=in_dict(filter,AdrePortDest); if(test==0) fichiersysAck <<AdrePortDest; } } else // sinon continuer = false; // sortir de la boucle de lecture } } } void insert_word(unsigned char filter[], string str) { unsigned int hash[NUM_HASHES]; int i; get_hashes(hash, str); for (i = 0; i < NUM_HASHES; i++) { hash[i] = (hash[i] >> FILTER_SIZE) ^ (hash[i] & FILTER_BITMASK); filter[hash[i] >> 3] |= 1 << (hash[i] & 7); } } int in_dict(unsigned char filter[], string str) { unsigned int hash[NUM_HASHES]; int i; get_hashes(hash, str); for (i = 0; i < NUM_HASHES; i++) { hash[i] = (hash[i] >> FILTER_SIZE) ^ (hash[i] & FILTER_BITMASK); if (!(filter[hash[i] >> 3] & (1 << (hash[i] & 7)))) return 0; } return 1; } void get_hashes(unsigned int hash[], string in) { unsigned char *str = (unsigned char *)in;//voici ce que je veux changer par sa equivalente en string int pos = strlen(in);// et celle la aussi convertir un sring en entier hash[0] = RSHash (str, pos); hash[1] = DJBHash (str, pos); hash[2] = FNVHash (str, pos); hash[3] = JSHash (str, pos); hash[4] = PJWHash (str, pos); hash[5] = SDBMHash(str, pos); hash[6] = DEKHash (str, pos); } /****************\ | Hash Functions | \****************/ unsigned int RSHash(unsigned char *str, unsigned int len) { unsigned int b = 378551; unsigned int a = 63689; unsigned int hash = 0; unsigned int i = 0; for(i = 0; i < len; str++, i++) { hash = hash * a + (*str); a = a * b; } return hash; } unsigned int JSHash(unsigned char *str, unsigned int len) { unsigned int hash = 1315423911; unsigned int i = 0; for(i = 0; i < len; str++, i++) { hash ^= ((hash << 5) + (*str) + (hash >> 2)); } return hash; } unsigned int PJWHash(unsigned char *str, unsigned int len) { const unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8); const unsigned int ThreeQuarters = (unsigned int)((BitsInUnsignedInt * 3) / 4); const unsigned int OneEighth = (unsigned int)(BitsInUnsignedInt / 8); const unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth); unsigned int hash = 0; unsigned int test = 0; unsigned int i = 0; for(i = 0; i < len; str++, i++) { hash = (hash << OneEighth) + (*str); if((test = hash & HighBits) != 0) { hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits)); } } return hash; } unsigned int SDBMHash(unsigned char *str, unsigned int len) { unsigned int hash = 0; unsigned int i = 0; for(i = 0; i < len; str++, i++) { hash = (*str) + (hash << 6) + (hash << 16) - hash; } return hash; } unsigned int DJBHash(unsigned char *str, unsigned int len) { unsigned int hash = 5381; unsigned int i = 0; for(i = 0; i < len; str++, i++) { hash = ((hash << 5) + hash) + (*str); } return hash; } unsigned int DEKHash(unsigned char *str, unsigned int len) { unsigned int hash = len; unsigned int i = 0; for(i = 0; i < len; str++, i++) { hash = ((hash << 5) ^ (hash >> 27)) ^ (*str); } return hash; } unsigned int FNVHash(unsigned char *str, unsigned int len) { const unsigned int fnv_prime = 0x811C9DC5; unsigned int hash = 0; unsigned int i = 0; for(i = 0; i < len; str++, i++) { hash *= fnv_prime; hash ^= (*str); } return hash; }
Je posterai un code corrigé d'ici quelques minutes.
[EDIT]Voici le code.
Par contre, je n'ai pas compris si in_dict devait retourner un booléen, ou bien si elle retournait 0 en cas de succès.Code:
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243 #include <iostream> #include <fstream> #include <string> using namespace std; #define FILTER_SIZE 10 #define NUM_HASHES 7 #define WORD_BUF_SIZE 32 #define FILTER_SIZE_BYTES (1 << (FILTER_SIZE - 3)) #define FILTER_BITMASK ((1 << FILTER_SIZE) - 1) /* function de hashage */ unsigned int RSHash (unsigned char const *, size_t); unsigned int DJBHash (unsigned char const *, size_t); unsigned int FNVHash (unsigned char const *, size_t); unsigned int JSHash (unsigned char const *, size_t); unsigned int PJWHash (unsigned char const *, size_t); unsigned int SDBMHash(unsigned char const *, size_t); unsigned int DEKHash (unsigned char const *, size_t); /* fonction de help */ void Verifir_Sys(unsigned char[], char const *file, char const *file1); void insert_word(unsigned char[], string word); int in_dict(unsigned const char[], string word); void get_hashes(unsigned int[], string const & word); int main(int argc, char *argv[]) { unsigned char filter[FILTER_SIZE_BYTES]; if(argc < 3){ cout<<"le nombre de parametre a fournir au fonction est insuffisant"<<endl; } else Verifir_Sys(filter,argv[1],argv[2]); return 1; } void Verifir_Sys(unsigned char filter[], char const *filename, char const *filename1) { string AdrePortSour; string AdrePortDest; int test; fstream fichier(filename); fstream fichiersysAck(filename1); if ( !fichier ) { // si le fichier n'est pas trouvé cout << "fichier inexistant"; } else { // sinon bool continuer = true; // indicateur de fin de fichier while( continuer ) { string ch; // chaine contenant une ligne du fichier fichier >> ch; // ranger une ligne dans ch if ( ch != "" ){ if(ch=="S"){ //si la ligne n'est pas vide faire fichier >> ch; AdrePortSour=AdrePortSour+ch; fichier >> ch; AdrePortSour=AdrePortSour+" "+ch; cout<<AdrePortSour<<endl; AdrePortSour=""; insert_word(filter,AdrePortSour); } if(ch=="SA"){ //si la ligne n'est pas vide faire fichier >> ch; fichier >> ch; fichier >> ch; fichier >> ch; AdrePortDest=AdrePortDest+ch; fichier >> ch; AdrePortDest=AdrePortDest+" "+ch; cout<<AdrePortDest<<endl; AdrePortDest=""; test=in_dict(filter,AdrePortDest); if(test==0) fichiersysAck <<AdrePortDest; } } else // sinon continuer = false; // sortir de la boucle de lecture }//while } } void insert_word(unsigned char filter[], string str) { unsigned int hash[NUM_HASHES]; int i; get_hashes(hash, str); for (i = 0; i < NUM_HASHES; i++) { hash[i] = (hash[i] >> FILTER_SIZE) ^ (hash[i] & FILTER_BITMASK); filter[hash[i] >> 3] |= 1 << (hash[i] & 7); } } int in_dict(unsigned char const filter[], string str) { unsigned int hash[NUM_HASHES]; int i; get_hashes(hash, str); for (i = 0; i < NUM_HASHES; i++) { hash[i] = (hash[i] >> FILTER_SIZE) ^ (hash[i] & FILTER_BITMASK); if (!(filter[hash[i] >> 3] & (1 << (hash[i] & 7)))) return 0; } return 1; } void get_hashes(unsigned int hash[], std::string const &in) { unsigned char const *str = reinterpret_cast<unsigned char const*>(in.c_str()); size_t pos = in.length(); hash[0] = RSHash (str, pos); hash[1] = DJBHash (str, pos); hash[2] = FNVHash (str, pos); hash[3] = JSHash (str, pos); hash[4] = PJWHash (str, pos); hash[5] = SDBMHash(str, pos); hash[6] = DEKHash (str, pos); } /****************\ | Hash Functions | \****************/ unsigned int RSHash(unsigned char const *str, size_t len) { unsigned int b = 378551; unsigned int a = 63689; unsigned int hash = 0; size_t i = 0; for(i = 0; i < len; str++, i++) { hash = hash * a + (*str); a = a * b; } return hash; } unsigned int JSHash(unsigned char const *str, size_t len) { unsigned int hash = 1315423911; size_t i = 0; for(i = 0; i < len; str++, i++) { hash ^= ((hash << 5) + (*str) + (hash >> 2)); } return hash; } unsigned int PJWHash(unsigned char const *str, size_t len) { const unsigned int BitsInUnsignedInt = static_cast<unsigned int>(sizeof(unsigned int)) * 8; const unsigned int ThreeQuarters = ((BitsInUnsignedInt * 3) / 4); const unsigned int OneEighth = (BitsInUnsignedInt / 8); const unsigned int HighBits = static_cast<unsigned int>(0xFFFFFFFFu) << (BitsInUnsignedInt - OneEighth); unsigned int hash = 0; unsigned int test = 0; size_t i = 0; for(i = 0; i < len; str++, i++) { hash = (hash << OneEighth) + (*str); if((test = hash & HighBits) != 0) { hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits)); } } return hash; } unsigned int SDBMHash(unsigned char const *str, size_t len) { unsigned int hash = 0; size_t i = 0; for(i = 0; i < len; str++, i++) { hash = (*str) + (hash << 6) + (hash << 16) - hash; } return hash; } unsigned int DJBHash(unsigned char const *str, size_t len) { unsigned int hash = 5381; size_t i = 0; for(i = 0; i < len; str++, i++) { hash = ((hash << 5) + hash) + (*str); } return hash; } unsigned int DEKHash(unsigned char const *str, size_t len) { unsigned int hash = static_cast<unsigned int>(len); size_t i = 0; for(i = 0; i < len; str++, i++) { hash = ((hash << 5) ^ (hash >> 27)) ^ (*str); } return hash; } unsigned int FNVHash(unsigned char const *str, size_t len) { const unsigned int fnv_prime = 0x811C9DC5u; unsigned int hash = 0; size_t i = 0; for(i = 0; i < len; str++, i++) { hash *= fnv_prime; hash ^= (*str); } return hash; }
Dans ce cas, il faudrait changer son type de retour en bool, et retourner true et false à la place de 0 et 1.
Et dans la fonction qui l'appelle, déclarer aussi la variable test avec le type bool, et transformer if(test==0) en if(!test).
Quand j'ai posté ce code, il compilait sans erreurs.
Poste tes messages d'erreurs. Pourquoi les gens s'attendent-ils toujours à ce que le gars en face ait une boule de cristal sur lui?