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.运行测试

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

相关推荐
csdn_aspnet1 小时前
Java 霍尔分区算法(Hoare‘s Partition Algorithm)
java·开发语言·算法
诸葛务农1 小时前
道路行驶条件下电动汽车永磁电机的有效使用寿命及永磁体的失效和回收再利用(下)
java·开发语言·算法
oort1232 小时前
VLStream:全开源决策式AI视频平台,赋能企业构建自主可控、降本增效的智能视觉应用介绍
大数据·开发语言·人工智能·开源·音视频·数据库架构
c238562 小时前
c/c++中的多态(上)
开发语言·c++
彷徨而立2 小时前
【C++】介绍 std::ifstream 输入文件流
开发语言·c++
罗超驿2 小时前
13.JavaScript 新手入门指南:语法、变量、流程控制全解析
开发语言·javascript
yingjie1102 小时前
Scanpy vs Seurat 深度对比:Python 与 R 的单细胞分析框架谁更强?
开发语言·python·r语言·生物信息学·单细胞转录组·seurat·scanpy
程序大视界3 小时前
【C++ 从基础到项目实战】C++(六):拷贝控制——浅拷贝与深拷贝,兼谈智能指针
开发语言·c++·cpp
luck_bor3 小时前
IO流知识点笔记
java·开发语言·笔记