pub trait HexDisplay {
fn to_hex(&self, chunk_size: usize) -> String;
}
static CHARS: &'static[u8] = b"0123456789abcdef";
impl HexDisplay for [u8] {
#[allow(unused_variables)]
fn to_hex(&self, chunk_size: usize) -> String {
let mut v = Vec::with_capacity(self.len() * 3);
let mut i = 0;
for chunk in self.chunks(chunk_size) {
let s = format!("{:08x}", i);
for &ch in s.as_bytes().iter() {
v.push(ch);
}
v.push('\t' as u8);
i = i + chunk_size;
for &byte in chunk {
v.push(CHARS[(byte >> 4) as usize]);
v.push(CHARS[(byte & 0xf) as usize]);
v.push(' ' as u8);
}
if chunk_size > chunk.len() {
for j in 0..(chunk_size - chunk.len()) {
v.push(' ' as u8);
v.push(' ' as u8);
v.push(' ' as u8);
}
}
v.push('\t' as u8);
for &byte in chunk {
if (byte >=32 && byte <= 126) || byte >= 128 {
v.push(byte);
} else {
v.push('.' as u8);
}
}
v.push('\n' as u8);
}
unsafe {
String::from_utf8_unchecked(v)
}
}
}