C is so much better.

But what i came here to do:

I wrote this base conversion program in C. I was going to use it for encryption.... (not just base conversion, but that's a part of the algorithm). Today i tried porting it to C++. I have absolutely no clue what's wrong with it, but any help would be appreciated.

Sorry for the lack of comments, if you want an explanation about a certain part just post here and i'll explain.

Compile with 'g++ -o btob btob.cpp' (or whatever compiler you use / whatever file you choose to save it as).

It compiles fine, screws up running........ no error message. and gdb isn't helping me this time.

And here's the WORKING version in C:Code:#include <iostream> #include <cmath> using namespace std; char toch(int w) { if (w > 9) return (w + 55); else return (w + 48); } int toi(char w) { if (w > 64) return (w - 55); else return (w - 48); } string revstr(string str) { char tmp; int l = str.length(), l2 = l - 1, ong; for (ong = 0; ong < (l / 2); ong ++) { tmp = str[ong]; str[ong] = str[l2 - ong]; str[l2 - ong] = tmp; } return str; } string itob(int n, int r) { string blah; int m; char tmp; int l, l2, ong; for (m = 0; (n % (int)pow((float)r, (float)m)) != n; m++) { blah[m] = toch((n % (int)pow((float)r, (float)(m + 1))) / (int)pow((float)r, (float)m)); n -= n % (int)pow((float)r, (float)(m + 1)); } blah[m + 1] = toch((n % (int)pow((float)r, (float)(m + 1))) / (int)pow((float)r, (float)m)); return revstr(blah); } int btoi(string num, int r) { int l = num.length(), ans = 0, ong; for (ong = 1; l - ong >= 0; ong++) ans += toi(num[l - ong]) * (int)pow((float)r, (float)(ong - 1)); return ans; } int main() { int b1, b2; string num; cout << "FIRST BASE? "; cin >> b1; cout << "SECOND BASE? "; cin >> b2; cout << "NUMBER? "; cin >> num; cout << itob(btoi(num, b1), b2) << endl; return 0; }

(compile with 'gcc -lm -o btob btob.c'.... replace names as needed)

Sorry for the incredibly long-ish post.Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> char toch(int w); int leng(char* str); void revstr(char* str); char* itob(int n, int r); int btoi(char* num, int r); char toch(int w) { if (w > 9) return (w + 55); else return (w + 48); } int toi(char w) { if (w > 64) return (w - 55); else return (w - 48); } int leng(char* str) { int x; for (x = 0; str[x] != 0; x++) {} return x; } void revstr(char* str) { char tmp; int l = leng(str), l2 = l - 1, ong; for (ong = 0; ong < (l / 2); ong++) { tmp = str[ong]; str[ong] = str[l2 - ong]; str[l2 - ong] = tmp; } } char* itob(int n, int r) { char* tmpstr = malloc(sizeof(char) * 1024); int m; for (m = 0; (n % (int)pow(r, m)) != n; m++) { tmpstr[m] = toch((n % (int)pow(r, m + 1)) / (int)pow(r, m)); n -= n % (int)pow(r, m + 1); } tmpstr[m+1] = toch((n % (int)pow(r, m+1)) / (int)pow(r, m)); revstr(tmpstr); return tmpstr; } int btoi(char* num, int r) { int l = leng(num), ans = 0, ong; for (ong = 1; l - ong >= 0; ong++) { ans += toi(num[l - ong]) * (int)pow(r, ong - 1); } return ans; } int main() { char num1[20]; int b1, b2; printf("FIRST BASE? "); scanf("%d", &b1); printf("FIRST NUM? "); scanf("%s", num1); printf("SECOND BASE? "); scanf("%d", &b2); printf("%s\n", itob(btoi(num1, b1), b2)); return 0; }

P.S. if you decidce you want to use my base conversion algorithm for some reason, and copy it instead of writing your own.... please give me a little credit. I know this is a pretty insignificant program but it was hard for me to write.