mirror of
https://github.com/shiroyashik/sculptor.git
synced 2025-12-06 04:51:13 +03:00
do it the right way
This commit is contained in:
parent
36b4a79a7e
commit
e6f3a4f7a4
1 changed files with 2 additions and 15 deletions
17
src/utils.rs
17
src/utils.rs
|
|
@ -20,23 +20,10 @@ pub fn rand() -> [u8; 50] {
|
||||||
pub fn bytes_into_string(code: &[u8]) -> String {
|
pub fn bytes_into_string(code: &[u8]) -> String {
|
||||||
// This *might* be the correct way to do it.
|
// This *might* be the correct way to do it.
|
||||||
|
|
||||||
// code.iter().map(|byte| format!("{:02x}", byte)).collect::<String>() // ????? Why do you need this? Why not just String::from_utf8()??
|
// code.iter().map(|byte| format!("{:02x}", byte)).collect::<String>() // ????? Why do you need this? Why not just hex::encode?
|
||||||
// So we need to turn each byte into a string with a 2-digit hexadecimal representation apparently...
|
// So we need to turn each byte into a string with a 2-digit hexadecimal representation apparently...
|
||||||
|
|
||||||
let test_res = hex::encode(code);
|
hex::encode(code) // This is the correct way to do it.
|
||||||
|
|
||||||
let res = code.iter().fold(String::new(), |mut acc, byte| {
|
|
||||||
acc.push_str(&format!("{:02x}", byte));
|
|
||||||
acc
|
|
||||||
}); // This is the same as the above, but with a fold instead of a map
|
|
||||||
|
|
||||||
|
|
||||||
tracing::debug!(?test_res, ?res, "bytes_into_string");
|
|
||||||
|
|
||||||
// Can we do this with hex::encode instead?
|
|
||||||
|
|
||||||
|
|
||||||
res
|
|
||||||
|
|
||||||
// String::from_utf8_lossy(code).to_string() // Tried this, causes corrupted string
|
// String::from_utf8_lossy(code).to_string() // Tried this, causes corrupted string
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue