Base conversion in plain clojure

Whoever write some computer code must know that number can be represented in different bases, one of those bases is base 10, another one is base 2 (binary) and other important bases are base 8 (octal) and base 16 (hexadecimal).

Whoever write computer code should be able to convert from a base to another one, this is such a common task that in high-level language is given for granted.

Clojure itself make so easy to bring number in base 10 from every base that no one think any more about this trivial task.

(int 16rCACCA)
;; => 830666

However change of base is a must know skill and I catch myself not sure enough about the implementation of such algorithm.

So I have re-implemented 2 simple function that convert from base 10 to base n and from base n to base 10.

I am sure that everybody know the theory behind such function, and I am also sure that none will have difficulties to implement such algorithm with a quick google search.

But are you sure you can implement it right now ? Not goggling allow, only clojure (or whatever language) and a piece of paper.

What need to be implemented are 2 functions, that behave like so:

(base-10-to-base-n 3452 17)
;; => "BG1"
(base-n-to-base-10 "BG1" 17)
;; => 3452
;;;; and just to be sure
(int 17rBG1)
;; => 3452

base-10-to-base-n convert the first integer in a number using like base the second argument.

base-n-to-base-10 is the other way around.

My simple solutions is here:

Base conversion in Clojure

Please feel free to comment with your own solution.

Now another question raise:

how to convert a number from base n to base m, without pass to base 10 ?

I wasn’t able to answer, even using google, if you have a solution feel free to share.

I have bothered you with this little challenge just because I have too much free time, if you need some clojure/python freelancer I am for hire, feel free to drop me an email or tweet me @siscia_.

I am available for freelance work, I am specialized in IoT and distributed fault tolerant systems, if you are interested in working with me you can get in touch here: simone [at] mweb [dot] biz