-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "Encode and decode base32" example
- Loading branch information
Showing
3 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
|
||
{{#include string/hex.md}} | ||
|
||
{{#include string/base32.md}} | ||
|
||
{{#include string/base64.md}} | ||
|
||
{{#include ../links.md}} |