Add "Encode and decode base32" example

This commit is contained in:
ia0 2022-01-15 16:05:29 +01:00
parent 752b035c1a
commit c14664ae54
3 changed files with 35 additions and 0 deletions

View file

@ -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] |
@ -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

View file

@ -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/

View file

@ -6,6 +6,8 @@
{{#include string/hex.md}}
{{#include string/base32.md}}
{{#include string/base64.md}}
{{#include ../links.md}}