utils.rs 522 B

12345678910111213
  1. pub fn str_to_bytes(name: &str) -> [u8; 32] {
  2. let mut name_bytes = [0u8; 32];
  3. name_bytes[..name.len()].copy_from_slice(name.as_bytes());
  4. name_bytes
  5. }
  6. pub fn bytes_to_str(bytes: &[u8; 32]) -> String {
  7. // Find the first occurrence of 0 (null terminator) or take all bytes if no null found
  8. let length = bytes.iter().position(|&x| x == 0).unwrap_or(bytes.len());
  9. // Convert the slice up to the null terminator (or full length) to a string
  10. String::from_utf8_lossy(&bytes[..length]).into_owned()
  11. }