You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.3 KiB

12 months ago
# -*- encoding: utf-8 -*-
"""
@File : disk_to_bin.py
@License : (C)Copyright 2021-2023
@Modify Time @Author @Version @Description
------------ ------- -------- -----------
2023/10/9 16:01 zart20 1.0 None
"""
# 定义常量
SECTOR_SIZE = 512 # 扇区大小
BYTES_PER_LINE = 16 # 每行字节数
MAX_SECTORS_TO_READ = 15927 # 要读取的最大扇区数量
# MAX_SECTORS_TO_READ = 3
# 打开设备文件
drive_letter = "I:" # 请替换为你要操作的磁盘
file_path = f"\\\\.\\{drive_letter}" # 设备文件路径
with open("output.bin","wb+") as out:
with open(file_path, 'rb') as disk:
sector_number = 0 # 起始扇区号
byte_offset = 0 # 字节偏移量初始化
sectors_read = 0 # 已读取的扇区数量
while sectors_read <= MAX_SECTORS_TO_READ:
# 读取扇区数据
disk.seek(sector_number * SECTOR_SIZE)
sector_data = disk.read(SECTOR_SIZE)
print(sector_data)
out.write(sector_data)
for byte in sector_data:
print(f"{byte:02X}", end=" ") # 输出每个字节的16进制表示
print() # 换行
sector_number += 1
byte_offset += len(sector_data) # 累加字节偏移量
sectors_read += 1