import sys
import os

def convert_exe_to_hex():
    # 1. 检查是否提供了文件名参数
    if len(sys.argv) < 2:
        print("用法: python script.py <exe文件路径>")
        return

    exe_path = sys.argv[1]

    # 2. 检查文件是否存在
    if not os.path.exists(exe_path):
        print(f"错误: 找不到文件 '{exe_path}'")
        return

    try:
        # 3. 生成默认的输出文件名 (例如: test.exe -> test.txt)
        file_name = os.path.splitext(exe_path)[0] # 获取不带后缀的文件名
        txt_path = f"{file_name}.txt"

        print(f"正在读取: {exe_path} ...")
        
        # 4. 执行转换 (采用分块读取，防止大文件撑爆内存)
        with open(exe_path, 'rb') as f_in, open(txt_path, 'w') as f_out:
            while chunk := f_in.read(1024 * 1024): # 每次处理 1MB
                f_out.write(chunk.hex().upper())

        print("-" * 30)
        print(f"转换成功！")
        print(f"输出文件: {txt_path}")
        
    except Exception as e:
        print(f"处理过程中出错: {e}")

if __name__ == "__main__":
    convert_exe_to_hex()