print_exe_hex

Function print_exe_hex 

Source
pub fn print_exe_hex(file_path: &str) -> Result<(), String>
Expand description

Print exe as hex to console

Examples found in repository?
examples/hex_converter.rs (line 47)
25fn main() {
26    init();
27
28    let args: Vec<String> = env::args().collect();
29
30    if args.len() < 3 {
31        print_usage(&args[0]);
32        return;
33    }
34
35    let command = &args[1];
36    let file_path = &args[2];
37
38    // Check if file exists
39    if !std::path::Path::new(file_path).exists() {
40        eprintln!("[-] File not found: {}", file_path);
41        return;
42    }
43
44    match command.as_str() {
45        "print" => {
46            println!("[*] Converting {} to Rust byte array:\n", file_path);
47            if let Err(e) = print_exe_hex(file_path) {
48                eprintln!("[-] Error: {}", e);
49            }
50        }
51
52        "string" => {
53            println!("[*] Converting {} to hex string:\n", file_path);
54            match exe_to_hex_string(file_path) {
55                Ok(hex) => println!("{}", hex),
56                Err(e) => eprintln!("[-] Error: {}", e),
57            }
58        }
59
60        "array" => {
61            println!("[*] Converting {} to formatted array:\n", file_path);
62            match exe_to_hex_array(file_path) {
63                Ok(hex) => println!("{}", hex),
64                Err(e) => eprintln!("[-] Error: {}", e),
65            }
66        }
67
68        "save" => {
69            let output_path = format!("{}.hex.txt", file_path);
70            println!("[*] Converting {} and saving to {}", file_path, output_path);
71
72            match fs::read(file_path) {
73                Ok(bytes) => {
74                    let hex = bytes_to_hex_string(&bytes);
75                    let content = format!(
76                        "// File: {}\n// Size: {} bytes\n\nconst PAYLOAD: &[u8] = &[\n    {}\n];\n",
77                        file_path,
78                        bytes.len(),
79                        hex
80                    );
81
82                    match fs::write(&output_path, content) {
83                        Ok(_) => println!("[+] Saved to {}", output_path),
84                        Err(e) => eprintln!("[-] Failed to save: {}", e),
85                    }
86                }
87                Err(e) => eprintln!("[-] Failed to read file: {}", e),
88            }
89        }
90
91        _ => {
92            eprintln!("[-] Unknown command: {}", command);
93            println!();
94            print_usage(&args[0]);
95        }
96    }
97}