diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2008-01-27 11:37:44 +0100 |
---|---|---|
committer | Tobias Klauser <tklauser@xenon.tklauser.home> | 2008-01-27 11:37:44 +0100 |
commit | 7e0f021a9aec35fd8e6725e87e3313b101d26f5e (patch) | |
tree | b1cacc4b24393f517aeb4610e9e1021f954307a8 /reference/C/CONTRIB/SNIP/rdxcnvrt.c |
Initial import (2.0.2-6)2.0.2-6
Diffstat (limited to 'reference/C/CONTRIB/SNIP/rdxcnvrt.c')
-rwxr-xr-x | reference/C/CONTRIB/SNIP/rdxcnvrt.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/reference/C/CONTRIB/SNIP/rdxcnvrt.c b/reference/C/CONTRIB/SNIP/rdxcnvrt.c new file mode 100755 index 0000000..3436483 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/rdxcnvrt.c @@ -0,0 +1,49 @@ +/*
+** RDXCNVRT.C - Convert between number bases
+**
+** public domain demo by Bob Stout
+*/
+
+#include <stdlib.h>
+#ifdef TEST
+ #include <stdio.h>
+#endif
+
+/*
+** Calling parameters: 1 - Number string to be converted
+** 2 - Buffer for the converted output
+** 3 - Radix (base) of the input
+** 4 - Radix of the output
+**
+** Returns: Pointer to converted output
+*/
+
+char *radix_convert(const char *in, char *out, int rin, int rout)
+{
+ long n;
+ char *dummy;
+
+ n = strtol(in, &dummy, rin);
+ return ltoa(n, out, rout);
+}
+
+#ifdef TEST
+
+int main(int argc, char *argv[])
+{
+ int rin, rout;
+ char buf[40];
+
+ if (4 > argc)
+ {
+ puts("Usage: RDXCNVRT <number> <base_in> <base_out>");
+ return(-1);
+ }
+ rin = atoi(argv[2]);
+ rout = atoi(argv[3]);
+ printf("%s (base %d) = %s (base %d)\n", argv[1], rin,
+ radix_convert((const char *)argv[1], buf, rin, rout), rout);
+ return 0;
+}
+
+#endif
|