RE: [Challenge] All your bijective base are belong to us 09-06-2015, 10:16 PM
#5
Here's a quick solution in C:
Left out user input for the values as I didn't see it as trivial to the main code itself.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX_BASE 64
static char M[] = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz<=>";
const char *base10_to_b(int b, int n, char *buf)
{
int i = MAX_BASE - 1;
while (n)
{
--n;
buf[i--] = M[n % b];
n /= b;
}
buf[MAX_BASE] = 0;
return &buf[i] + 1;
}
int baseb_to_10(int b, const char *buf)
{
int result = 0;
const char *pch = NULL, *p = buf;
int e = strlen(buf);
while (*p)
{
if ((pch = strchr(M, *p)))
result += (pch - M + 1) * (int)pow(b, --e);
++p;
}
return result;
}
int main(void)
{
int inbase, outbase;
char input_n[256];
char *pch = NULL;
char outbuf[MAX_BASE] = { 0 };
inbase = '3';
strcpy(input_n, "31");
outbase = '1';
if (!(pch = strchr(M, inbase))) exit(1);
inbase = pch - M + 1;
if (!(pch = strchr(M, outbase))) exit(1);
outbase = pch - M + 1;
puts(base10_to_b(outbase, baseb_to_10(inbase, input_n), outbuf));
exit(0);
}Left out user input for the values as I didn't see it as trivial to the main code itself.
- mostly braindead monkeys on this forum.

![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)