68 lines
1.5 KiB
Rust
68 lines
1.5 KiB
Rust
pub fn get_hex(prefix_len: u32, data: Vec<u8>) -> String {
|
|
let mut header_begin = prefix_len >> 4;
|
|
let mut counter = prefix_len % 16;
|
|
// println!("{}", header_begin);
|
|
// println!("{}", counter);
|
|
let mut ret = String::new();
|
|
|
|
for i in data {
|
|
if counter % 16 == 0 {
|
|
counter = 0;
|
|
// if header_begin != 0 {
|
|
// ret.push('\n');
|
|
// }
|
|
header_begin += 1;
|
|
ret += &format!("{:04x}\t", header_begin - 1);
|
|
}
|
|
counter += 1;
|
|
ret = ret + &*format!("{:02x} ", i);
|
|
if counter % 16 ==0 {
|
|
ret.push('\n');
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
pub fn get_ascii(prefix_len: u32, data: Vec<u8>) -> String {
|
|
let mut header_begin = prefix_len >> 4;
|
|
let mut counter = prefix_len % 16;
|
|
// println!("{}", header_begin);
|
|
// println!("{}", counter);
|
|
let mut ret = String::new();
|
|
|
|
for i in data {
|
|
if counter % 16 == 0 {
|
|
counter = 0;
|
|
// if header_begin != 0 { ret.push('\n'); }
|
|
header_begin += 1;
|
|
ret += &format!("{:04x}\t", header_begin - 1);
|
|
}
|
|
counter += 1;
|
|
|
|
if i.is_ascii_alphanumeric() || i.is_ascii_punctuation() || i.is_ascii_graphic() {
|
|
// ret += &*(i as char).to_string();
|
|
ret.push(char::from(i));
|
|
// basic_show.push(i as char);
|
|
} else {
|
|
ret += ".";
|
|
}
|
|
|
|
if counter % 16 ==0 {
|
|
ret.push('\n');
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use crate::hexdump::get_hex;
|
|
|
|
#[test]
|
|
fn exploration() {
|
|
let data = vec![10, 20, 203];
|
|
let tmp = get_hex(14, data);
|
|
println!("{}", tmp);
|
|
// assert_eq!(2 + 2, 4);
|
|
}
|
|
} |