Skip to content

Commit

Permalink
Add "Encode and decode base32" example
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 committed Jan 15, 2022
1 parent 752b035 commit c14664a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
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}}

0 comments on commit c14664a

Please sign in to comment.