diff options
Diffstat (limited to 'src/utils.rs')
-rw-r--r-- | src/utils.rs | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/utils.rs b/src/utils.rs index fb9b812..25470e7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,11 +1,19 @@ use crate::ReadSeek; use byteorder::ReadBytesExt; use std::char::decode_utf16; +use std::fmt::Write; use std::io; pub fn to_hex_string(bytes: &[u8]) -> String { - let strs: Vec<String> = bytes.iter().map(|b| format!("{:02X}", b)).collect(); - strs.join("") + let len = bytes.len(); + // Each byte is represented by 2 ascii bytes. + let mut s = String::with_capacity(len * 2); + + for byte in bytes { + write!(s, "{:02X}", byte).expect("Writing to an allocated string cannot fail"); + } + + s } /// Reads a utf16 string from the given stream. |