Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Encode and decode base32" example #654

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| [Percent-encode a string][ex-percent-encode] | [![percent-encoding-badge]][url] | [![cat-encoding-badge]][cat-encoding] |
| [Encode a string as application/x-www-form-urlencoded][ex-urlencoded] | [![url-badge]][url] | [![cat-encoding-badge]][cat-encoding] |
| [Encode and decode hex][ex-hex-encode-decode] | [![data-encoding-badge]][data-encoding] | [![cat-encoding-badge]][cat-encoding] |
| [Encode and decode base32][ex-base32] | [![data-encoding-badge]][data-encoding] | [![cat-encoding-badge]][cat-encoding] |
| [Encode and decode base64][ex-base64] | [![base64-badge]][base64] | [![cat-encoding-badge]][cat-encoding] |
| [Read CSV records][ex-csv-read] | [![csv-badge]][csv] | [![cat-encoding-badge]][cat-encoding] |
| [Read CSV records with different delimiter][ex-csv-delimiter] | [![csv-badge]][csv] | [![cat-encoding-badge]][cat-encoding] |
Expand All @@ -20,6 +21,7 @@
[ex-percent-encode]: encoding/strings.html#percent-encode-a-string
[ex-urlencoded]: encoding/strings.html#encode-a-string-as-applicationx-www-form-urlencoded
[ex-hex-encode-decode]: encoding/strings.html#encode-and-decode-hex
[ex-base32]: encoding/strings.html#encode-and-decode-base32
[ex-base64]: encoding/strings.html#encode-and-decode-base64
[ex-csv-read]: encoding/csv.html#read-csv-records
[ex-csv-delimiter]: encoding/csv.html#read-csv-records-with-different-delimiter
Expand Down
31 changes: 31 additions & 0 deletions src/encoding/string/base32.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## Encode and decode base32

[![data-encoding-badge]][data-encoding] [![cat-encoding-badge]][cat-encoding]

The [`data_encoding`] crate provides a `BASE32::encode` method which takes a
`&[u8]` and returns a `String` containing the base32 representation of the data.

Similarly, a `BASE32::decode` method is provided which takes a `&[u8]` and
returns a `Vec<u8>` if the input data is successfully decoded.

The example below coverts `&[u8]` data to base32 equivalent and compares this
value to the expected value.

```rust,edition2018
use data_encoding::{BASE32, DecodeError};
fn main() -> Result<(), DecodeError> {
let original = b"Cooking with Rust";
let expected = "INXW623JNZTSA53JORUCAUTVON2A====";
let encoded = BASE32.encode(original);
assert_eq!(encoded, expected);
let decoded = BASE32.decode(encoded.as_bytes())?;
assert_eq!(&decoded[..], &original[..]);
Ok(())
}
```

[`data_encoding`]: https://docs.rs/data-encoding/*/data_encoding/
2 changes: 2 additions & 0 deletions src/encoding/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

{{#include string/hex.md}}

{{#include string/base32.md}}

{{#include string/base64.md}}

{{#include ../links.md}}