/radix-math

Clojure library to convert integers to and from arbitrary radixes.

Primary LanguageClojure

Clojure Radix Math

This library provides utilities to convert integers to arbitrary radix representations with custom "letter alphabets".

Likewise, strings can be easily decoded back into a integer representation.

While Java has a limit of base (or "radix") 36, this library is generic and can handle arbitrary bases. See the usage section for examples.

Releases and Dependency Information

radix-math is released via Clojars. The Latest stable release is 1.0.0.

Leiningen dependency information:

[radix-math "1.0.0"]

Usage

(require '[radix-math :as radix])

;; Lets start with base 10:
(radix/to-radix 1 (range 10))
; => (1)

;; 95 has two digits
(radix/to-radix 95 (range 10))
; => (9 5)

;; To convert to hex, lets first define all available digits:
(def hex (map #(.charAt (format "%x" %) 0) (range 16)))

(apply str (radix/to-radix 255 hex))
; => "ff"

;; Using all printable ASCII characters as digits:
(apply str (radix/to-radix 480203 radix/printable-ascii))
; => "WAT"

;; Converting back from sequences:
(radix/from-radix [9, 5] (range 10))
; => 95

(radix/from-radix "ff" hex)
; => 255

(radix/from-radix "WAT" radix/printable-ascii)
; => 480203

License

Copyright © 2014 Torsten Becker.

Distributed under the Eclipse Public License, the same as Clojure.