Skip to content

Commit

Permalink
add note on encoding/decoding integer values #14
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Apr 26, 2020
1 parent ec2cdf6 commit 07b11c5
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ and returns a Base58 encoded String. <br />

See the section [What is an Elixir binary?](#what-is-an-elixir-binary) for more information about the binary type in Elixir.

## How to use the module
## How to use the module?

1. Add the package to you dependencies
### 1. Add the package to you dependencies

```elixir
defp deps do
Expand All @@ -38,25 +38,48 @@ end

and run `mix deps.get`

2. Invoke the `encode` function with a binary as parameter:
### 2. Invoke the `encode` function with a binary as parameter:

```elixir
Base58.encode("foo")
"bQbp"
```

3. Invoke the `decode` function with a `base58` encoded string:
### 3. Invoke the `decode` function with a `base58` encoded string:

```elixir
Base58.encode("hello") |> Base58.decode()
"hello"
```

<br />


See `example/example.exs` file
for a simple example of how to use the module. <br />
You can also run this example with `mix run example/example.exs`


### Note: `Numeric` Values are Converted to `String`

When encoding an `Integer`
the value is converted to a `String` before encoding
so when it is decoded it will be a string e.g:
```elixir
Base58.encode(42) |> Base58.decode()
"42"
```
This isn't ideal if you expected an `Integer`
but there is no way to encode the **`type`** of the data.
So if you _know_ that you need it to be an `int`,
convert it back to a numeric value:
```elixir
{myInt, _} = Base58.encode(42) |> Base58.decode() |> Integer.parse()
42
```



<br /> <br />

## What is Base58?
Expand Down

0 comments on commit 07b11c5

Please sign in to comment.