I'm trying to solve this problem. The input file I used was the one on the site:
4
9 0123456789 oF8
Foo oF8 0123456789
13 0123456789abcdef 01
CODE O!CDE? A?JM!.
My abstract idea was:
Read the first line, loop the program that many times.
Read the next line. Load the data into three diffrent arrays.
Use the displace function with the first two arrays. Look here.
Convert the numbers to base 10.
Convert them to the output base.
Get there new values from the output characters, output them to new file.
Heres where I was before I put in the "tobase10" function:
#include <stdio.h>
#include <math.h>
#include <string.h>
void displace(int *output, char *string, char *chrset);
int main(int argc, char *argv[]) {
char chrnum[256];
char srclang[256];
char outlang[256];
int intnum[256];
int size;
int max;
int count = 0;
FILE *infile = fopen(argv[1], "r");
if(infile == NULL) {
printf("Error, Cannot open: %s\n", argv[1]);
return 1; }
fscanf(infile, "%d", &max);
while(count != max) {
fscanf(infile, "%s %s %s", chrnum, srclang, outlang);
displace(intnum, chrnum, srclang);
count++; }
fclose(infile);
printf("\nPress any key to quit ...");
getchar();
return 0; }
void displace(int *output, char *string, char *chrset) {
int count = 0;
while(string[count] != 0) {
output[count] = ((strchr(chrset, string[count])) - (int)chrset);
count++; } }
To test it, put the following after the line "displace(intnum, chrnum, srclang);" :
int count1 = 0;
printf("\nbase: %d\nsize: %d\n", strlen(srclang), strlen(chrnum));
while(count1 != strlen(chrnum)) {
printf("%d", intnum[count1]);
count1++; }
So everything up to there seems to be working. Heres the program I made to test the "tobase10" function:
#include <stdio.h>
#include <math.h>
#include <string.h>
int tobase10(int *input, int size, int base);
int main() {
int array[256] = {2, 0, 3, 4};
int count = 0;
int size;
size = tobase10(array, 4, 6);
while(count != size) {
printf("%d", array[count]);
count++; }
printf("\nPress any key to continue ...\n");
getchar();
return 0;}
int tobase10(int *input, int size, int base) {
int result = 0;
int count = 0;
int intsize;
while((size+1) != 0) {
result = (input[count] * pow(base, (size-1))) + result;
count++;
size--; }
intsize = ((int)log10(result)+1);
count = (intsize-1);
while(count >= 0) {
input[count] = result % 10;
result = result / 10;
count--; }
return intsize; }
It also works perfectly. But what happens when I join the two togeather?
I can't think of any reson of why it wouldent work:
#include <stdio.h>
#include <math.h>
#include <string.h>
void displace(int *output, char *string, char *chrset);
int tobase10(int *input, int size, int base);
int main(int argc, char *argv[]) {
char chrnum[256];
char srclang[256];
char outlang[256];
int intnum[256];
int size;
int max;
int count = 0;
FILE *infile = fopen(argv[1], "r");
if(infile == NULL) {
printf("Error, Cannot open: %s\n", argv[1]);
return 1; }
fscanf(infile, "%d", &max);
while(count != max) {
fscanf(infile, "%s %s %s", chrnum, srclang, outlang);
displace(intnum, chrnum, srclang);
size = tobase10(intnum, strlen(chrnum), strlen(srclang));
int count1 = 0;
while(count1 != size) {
printf("%d", intnum[count1]);
count1++; }
printf("\n");
count++; }
fclose(infile);
printf("\nPress any key to quit ...");
getchar();
return 0; }
void displace(int *output, char *string, char *chrset) {
int count = 0;
while(string[count] != 0) {
output[count] = ((strchr(chrset, string[count])) - (int)chrset);
count++; } }
int tobase10(int *input, int size, int base) {
int result = 0;
int count = 0;
int intsize;
while((size+1) != 0) {
result = (input[count] * pow(base, (size-1))) + result;
count++;
size--; }
intsize = ((int)log10(result)+1);
count = (intsize-1);
while(count >= 0) {
input[count] = result % 10;
result = result / 10;
count--; }
return intsize; }
It exploded in my face. whoa!
P.S. The code I used to output to the sceen is just for debugging. It's not ment to be good programing practece.