C# 命令行指令 查看二进制文件

1.代码

cs 复制代码
using System;
using System.IO;
using System.Linq;

class HexDump
{
    static void Main(string[] args)
    {
        // 解析命令行参数
        if (args.Length == 0 || args[0] == "/?" || args[0] == "-h" || args[0] == "--help")
        {
            ShowHelp();
            return;
        }

        string filePath = args[0];
        long offset = 0;

        // 处理偏移量参数(-o 或 --offset)
        for (int i = 1; i < args.Length; i++)
        {
            if (args[i] == "-o" || args[i] == "--offset")
            {
                if (i + 1 < args.Length && long.TryParse(args[i + 1], System.Globalization.NumberStyles.HexNumber, null, out offset))
                {
                    i++;
                }
                else if (i + 1 < args.Length && long.TryParse(args[i + 1], out offset))
                {
                    i++;
                }
                else
                {
                    Console.WriteLine("错误:无效的偏移量。");
                    ShowHelp();
                    return;
                }
            }
        }

        // 检查文件是否存在
        if (!File.Exists(filePath))
        {
            Console.WriteLine($"错误:文件 '{filePath}' 不存在。");
            return;
        }

        try
        {
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            using (BinaryReader reader = new BinaryReader(fs))
            {
                long fileSize = fs.Length;

                // 验证偏移量
                if (offset < 0) offset = 0;
                if (offset > fileSize) offset = fileSize;

                // 定位到偏移量
                fs.Seek(offset, SeekOrigin.Begin);

                int bytesPerLine = 16;
                int linesPerPage = 20;
                long currentOffset = offset;

                bool continueReading = true;

                while (continueReading && currentOffset < fileSize)
                {
                    int linesDisplayed = 0;

                    // 显示一页内容(20行)
                    while (linesDisplayed < linesPerPage && currentOffset < fileSize)
                    {
                        byte[] buffer = new byte[bytesPerLine];
                        int bytesRead = reader.Read(buffer, 0, bytesPerLine);

                        if (bytesRead == 0) break;

                        // 显示偏移量
                        Console.Write($"{currentOffset:X8}  ");

                        // 显示十六进制字节
                        for (int i = 0; i < bytesPerLine; i++)
                        {
                            if (i < bytesRead)
                                Console.Write($"{buffer[i]:X2} ");
                            else
                                Console.Write("   ");

                            if (i == 7) Console.Write(" ");
                        }

                        // 显示ASCII表示
                        Console.Write(" |");
                        for (int i = 0; i < bytesRead; i++)
                        {
                            char c = (char)buffer[i];
                            Console.Write(char.IsControl(c) ? '.' : c);
                        }
                        Console.WriteLine("|");

                        currentOffset += bytesRead;
                        linesDisplayed++;
                    }

                    // 判断是否还有更多内容
                    if (currentOffset < fileSize)
                    {
                        Console.WriteLine($"\n-- 显示第 {currentOffset / bytesPerLine - (currentOffset % bytesPerLine == 0 ? linesPerPage : currentOffset % bytesPerLine / bytesPerLine)} 页,按 Enter 键继续,按 Q 键退出 --");
                        var key = Console.ReadKey(true);
                        if (key.Key == ConsoleKey.Q)
                        {
                            continueReading = false;
                        }
                        Console.WriteLine(); // 换行准备下一页
                    }
                    else
                    {
                        Console.WriteLine("\n文件已显示完毕。");
                        continueReading = false;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"错误:{ex.Message}");
        }
    }

    static void ShowHelp()
    {
        Console.WriteLine("HexDump - 二进制文件十六进制查看器");
        Console.WriteLine("用法:");
        Console.WriteLine("  HexDump <文件路径> [-o <偏移量>]");
        Console.WriteLine("");
        Console.WriteLine("参数:");
        Console.WriteLine("  <文件路径>     要查看的二进制文件路径");
        Console.WriteLine("  -o, --offset   起始偏移量(支持十进制或十六进制,十六进制请加0x前缀)");
        Console.WriteLine("  /?, -h, --help 显示帮助信息");
        Console.WriteLine("");
        Console.WriteLine("示例:");
        Console.WriteLine("  HexDump test.bin");
        Console.WriteLine("  HexDump test.bin -o 256");
        Console.WriteLine("  HexDump test.bin -o 0x100");
        Console.WriteLine("");
        Console.WriteLine("操作说明:");
        Console.WriteLine("  Enter 键 - 继续显示下一页");
        Console.WriteLine("  Q 键     - 退出程序");
    }
}

2.运行测试

保存图片
编辑图片
移除物体
提取文字
图片翻译

相关推荐
李永奉几秒前
杰理可视化SDK开发-【BUG】AC7006F8芯片开启一拖二和双麦DNS通话算法后,大概率通话时蓝牙自动断连,RAM空间爆了问题处理
单片机·嵌入式硬件·mcu·物联网·电脑·bug
小灰灰搞电子几秒前
Qt 中使用 QSettings 存储结构体数据详解
开发语言·qt
外收内放6 分钟前
FastAPI猜字谜实战(5):会话管理
开发语言·python·学习·fastapi
Ai-_Man7 分钟前
Microsoft Copilot对话数据怎么导入到另外一个Microsoft Copilot账号里去?AI导出鸭
开发语言·javascript·人工智能·microsoft·chatgpt·ecmascript·copilot
小向是个Der8 分钟前
P02_你的变量住在哪里_教学文档
经验分享·笔记·stm32·单片机
H.莓飛12 分钟前
【数据结构】归并与外排序
linux·服务器·c语言·开发语言·数据结构·visual studio
技术老孙14 分钟前
给 Agent 装上对话框 —— AgentScope-Java 接入 Spring Boot 与 SSE 流式实战
java·开发语言·spring boot
深圳老胡25 分钟前
液面探测功能的硬件方案对比:原理、优缺点与选型建议
笔记·单片机
成成爱质评32 分钟前
Windows 任务计划程序 + 唤醒定时器:让电脑按时开机干活
windows·单片机·电脑
AMASENSYS艾马斯42 分钟前
低压变频器输入输出电抗器选型指南
驱动开发·单片机·机器学习·硬件工程·精益工程