diff --git a/src/encoding.md b/src/encoding.md index 3ed713ee..03aa7d8e 100644 --- a/src/encoding.md +++ b/src/encoding.md @@ -6,6 +6,7 @@ | [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 base64][ex-base64] | [![base64-badge]][base64] | [![cat-encoding-badge]][cat-encoding] | +| [Encode and decode base32][ex-base32] | [![data-encoding-badge]][data-encoding] | [![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] | | [Filter CSV records matching a predicate][ex-csv-filter] | [![csv-badge]][csv] | [![cat-encoding-badge]][cat-encoding] | @@ -21,6 +22,7 @@ [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-base64]: encoding/strings.html#encode-and-decode-base64 +[ex-base32]: encoding/strings.html#encode-and-decode-base32 [ex-csv-read]: encoding/csv.html#read-csv-records [ex-csv-delimiter]: encoding/csv.html#read-csv-records-with-different-delimiter [ex-csv-filter]: encoding/csv.html#filter-csv-records-matching-a-predicate diff --git a/src/encoding/string/base32.md b/src/encoding/string/base32.md new file mode 100644 index 00000000..6404ee2b --- /dev/null +++ b/src/encoding/string/base32.md @@ -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` 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/ diff --git a/src/encoding/strings.md b/src/encoding/strings.md index 6cbec02c..0944e648 100644 --- a/src/encoding/strings.md +++ b/src/encoding/strings.md @@ -8,4 +8,6 @@ {{#include string/base64.md}} +{{#include string/base32.md}} + {{#include ../links.md}}