Skip to content

Instantly share code, notes, and snippets.

@tommilligan
Created November 8, 2023 16:22
Show Gist options
  • Select an option

  • Save tommilligan/1b4bf3c44029383b8892fc83b89ca4b1 to your computer and use it in GitHub Desktop.

Select an option

Save tommilligan/1b4bf3c44029383b8892fc83b89ca4b1 to your computer and use it in GitHub Desktop.
Decoding UTF16LE String from Vec<u8>
extern crate encoding_rs; // 0.8.33
fn main() {
let data: Vec<u8> = vec![46, 0, 120, 0, 108, 0, 115, 0, 120, 0];
let mut buffer: String = String::with_capacity(data.len() * 2);
let mut decoder = encoding_rs::UTF_16LE.new_decoder();
let (coder_result, read, replaced) = decoder.decode_to_string(&data, &mut buffer, true);
assert!(coder_result == encoding_rs::CoderResult::InputEmpty);
println!("String: {}", buffer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment