news 2026/6/2 8:52:13

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C# 命令行指令 查看二进制文件

1.代码

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

保存图片
编辑图片
移除物体
提取文字
图片翻译
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/2 8:50:22

Win11文件拖拽卡顿?别急着重装,试试这3个隐藏设置(亲测有效)

Win11文件拖拽卡顿终极优化指南&#xff1a;从原理到实战的深度解决方案每次在Win11中拖拽文件时&#xff0c;那种卡顿感就像是在用老式拨号上网加载高清图片——明明硬件配置足够&#xff0c;却总有种说不出的迟滞。这种体验不仅影响工作效率&#xff0c;更会让人对系统的流畅…

作者头像 李华
网站建设 2026/6/2 8:46:19

构建防御性软件工程:从契约需求到自动化测试的严谨实践

1. 项目概述&#xff1a;当“严谨”成为软件开发的底层文化最近和几个在硅谷和班加罗尔都有团队的朋友聊天&#xff0c;他们不约而同地提到一个现象&#xff1a;一些来自印度的技术团队&#xff0c;在解决复杂软件工程问题时&#xff0c;展现出一种令人印象深刻的“系统性严谨”…

作者头像 李华
网站建设 2026/6/2 8:41:00

技术内容策展实战:从HackerNoon案例解析开发者社区邮件简报运营

1. 项目概述&#xff1a;当“午间简报”遇见“沙盒” 如果你和我一样&#xff0c;每天被海量的科技资讯、行业动态和深度分析文章淹没&#xff0c;却又担心错过真正有价值的内容&#xff0c;那么你一定能理解“信息筛选”本身已经成了一项繁重的工作。今天想和大家深入聊聊的&…

作者头像 李华