ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - whatyouhide/convertat: An Elixir library for converting from and to arbitrary bases.
An Elixir library for converting from and to arbitrary bases. - whatyouhide/convertat
Visit Site

GitHub - whatyouhide/convertat: An Elixir library for converting from and to arbitrary bases.

GitHub - whatyouhide/convertat: An Elixir library for converting from and to arbitrary bases.

Convertat

Build Status Coverage Status Package License

Convertat is a small Elixir library that provides functions for converting values from and to arbitrary bases.

Installation and docs

To use this library with Mix, just declare its dependency in the mix.exs file:

defp deps do
  [
    # Using the hex package manager:
    {:convertat, "~> 1.0"},
    # or grabbing the latest version (master branch) from GitHub:
    {:convertat, github: "whatyouhide/convertat"},
  ]
end

Then run mix deps.get. The documentation for the current and the older versions of Convertat can be found on its hex.pm page.

Usage

Convertat leverages on the power of the |> operator in order to provide a clean syntax for converting values between bases. The only two functions that it exports are Convertat.from_base/2 and Convertat.to_base/3.

For example, say we want to convert the binary value "11011" (27 in base 10) to its hex representation:

iex> "10110" |> Convertat.from_base(2) |> Convertat.to_base(16)
"1b"

That's pretty straightforward and, moreover, easily achievable using Elixir's standard library (String.to_integer/2 and to_string/1). In fact, when using integers as bases, you're limited to the standard 2..36 range.

What about this:

iex> "↑↓↑" |> Convertat.from_base(["↓", "↑"])
5

We just used a binary (it has two digits) base where the digits are the "↓" and "↑" strings.

Digits in list bases are listed from the least significant one to the most significant one; in the above example, would be 0 in the binary base while would be 1.

We can also use lists as values (instead of strings); this allows for some cool multicharacter-digits bases. Here's another binary base:

iex> ["foo", "bar"] |> Convertat.from_base(["bar", "foo"])
2

As you can see, digits significance in list values is the opposite from bases: the least significant digits are on the right, like they would be in written numbers.

While the from_base/2 function converts a value in an arbitrary base to an integer in base 10, the to_base/3 function does the opposite:

iex> 20 |> Convertat.to_base(["a", "b"])
"babaa"

As with from_base, bases can be integers (in the 2..36 range, just like with the standard library) or lists of digits.

By default, a string representation of the converted number is returned. You can also specify that you want a list:

iex> 16 |> Convertat.to_base(16, as_list: true)
["1", "0"]

This may seem useless, but think of the multichar-digits base from above:

iex> 2 |> Convertat.to_base(["bar", "foo"])
"foobar"

How can you parse "foobar" back into a base 10 value with the same ["bar", "foo"] base?

iex> base = ["bar", "foo"]
["bar", "foo"]
iex> val = 2 |> Convertat.to_base(base, as_list: true)
["foo", "bar"]
iex> val |> Convertat.from_base(base)
2

One more thing: if you're converting between bases a lot of times, consider importing the two functions for a uber-clean syntax:

iex> import Convertat, only: :functions
nil
iex> "1011" |> from_base(2) |> to_base(16)
"b"

Contributing

If you wish to contribute to this project, well, thanks! You know the deal:

  • fork the repository
  • make changes and add/update tests
  • make sure the tests pass by running mix test
  • commit changes
  • open a pull request

Feel free to open an issue if you find something wrong with the library.

License

MIT © 2014 Andrea Leopardi, see the license file

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory