Login Register


[Challenge] All your bijective base are belong to us filter_list
Author
Message
RE: [Challenge] All your bijective base are belong to us #5
Here's a quick solution in C:
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.

Reply





Messages In This Thread



Users browsing this thread: 1 Guest(s)