Rust
Rust
Demonstrate Binary Data Encoding Methods
Demonstrates binary data encoding methods.Note: This example requires Chilkat v9.5.0.64 or later.
Chilkat Rust Downloads
let bin_data = chilkat::BinData::new();
// Append 10 bytes: 00, 01, 02, 03, 04, 05, 06, 07, 08, 09
let _ = bin_data.append_encoded("00010203040506070809", "hex");
// Show that binData contains 10 bytes.
println!("num bytes = {}", bin_data.num_bytes());
// Get as base64
println!("base64: {}", bin_data.get_encoded("base64").unwrap_or_default());
// Get a chunk of the binary data.
// The 1st byte is at index 0.
// The output should be "02030405"
let mut offset = 2;
let mut num_bytes = 4;
println!("chunk: {}", bin_data.get_encoded_chunk(offset, num_bytes, "hex").unwrap_or_default());
// Copy the bytes to a StringBuilder using the base64url encoding
let sb = chilkat::StringBuilder::new();
let _ = bin_data.get_encoded_sb("base64url", &sb);
println!("base64url: {}", sb.get_as_string().unwrap_or_default());
// Remove a chunk from the binary data
offset = 2;
num_bytes = 4;
let _ = bin_data.remove_chunk(offset, num_bytes);
// The bytes remaining are 00,01,06,07,08,and 09
println!("after removing chunk: {}", bin_data.get_encoded("hex").unwrap_or_default());