Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // convert integer to an arbitrary position system
- // enumerate/generate password dictionary in lexicographic order
- #include <iostream>
- #include <string.h>
- #include <algorithm>
- using namespace std;
- char* conv(int x, char* dict, char* buf, int alpha=0) {
- int i = 0;
- int n = strlen(dict);
- if (alpha) x++;
- do {
- if (alpha) x--;
- buf[i++] = dict[x % n];
- x /= n;
- } while (x);
- buf[i] = 0;
- reverse(buf, buf+i);
- return buf;
- }
- int rconv(char * buf, char * dict, int alpha=0) {
- int i = 0;
- int n = strlen(dict);
- int k = strlen(buf);
- int x = 0;
- int pos = 1;
- if (alpha) x--;
- for (int i=k-1; i>=0; i--) {
- int index = strchr(dict, buf[i])-dict;
- if (alpha) index++;
- x += index*pos;
- pos *= n;
- }
- return x;
- }
- int main() {
- char dict[] = "abc";
- char buf1[256];
- char buf2[256];
- for (int i=0; i<20; i++) {
- conv(i, dict, buf1, 0);
- conv(i, dict, buf2, 1);
- int x = rconv(buf1, dict, 0);
- int y = rconv(buf2, dict, 1);
- cout << i << "\t"
- << buf1 << "\t" << x << "\t"
- << buf2 << "\t" << y << "\t" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement