当前位置: 首页 > news >正文

16进制数转32位浮点数/整数

闲暇时间制作了一个小工具,实现16进制数转整数或浮点数,按左右键可以调整字节顺序

图片

using System;
using System.Buffers.Binary;
using System.Reflection.Metadata;namespace Hex32ConvertTool
{class Program{// 四组字节序映射 0~3 对应原字节 [B0,B1,B2,B3]private static readonly int[][] _orderMap ={new[] {0, 1, 2, 3}, // ABCDnew[] {1, 0, 3, 2}, // BADCnew[] {2, 3, 0, 1}, // CDABnew[] {3, 2, 1, 0}  // DCBA
            };private static readonly string[] _orderName = { "ABCD", "BADC", "CDAB", "DCBA" };private static readonly string[] _typeName = { "int32 整数", "float 单精度浮点" };static void Main(string[] args){Console.OutputEncoding = System.Text.Encoding.UTF8;Console.InputEncoding = System.Text.Encoding.UTF8;Console.Title = "32位16进制转换器|字序切换";Console.CursorVisible = false;string hexStr = string.Empty;int selectType = 0;   // 0:int  1:floatint selectOrder = 0;  // 字节序选中项while (true){Console.Clear();DrawUI(hexStr, selectType, selectOrder);var key = Console.ReadKey(true);// ESC 清空重新输入if (key.Key == ConsoleKey.Escape){hexStr = string.Empty;continue;}// 切换类型 i 整数 / f 浮点if (key.Key == ConsoleKey.I) selectType = 0;if (key.Key == ConsoleKey.F) selectType = 1;// 左右箭头切换字节序if (key.Key == ConsoleKey.LeftArrow)selectOrder = selectOrder <= 0 ? 3 : selectOrder - 1;if (key.Key == ConsoleKey.RightArrow)selectOrder = selectOrder >= 3 ? 0 : selectOrder + 1;// 退格删除if (key.Key == ConsoleKey.Backspace && hexStr.Length > 0){hexStr = hexStr[..^1];continue;}// 只允许合法16进制字符,限制最大8位char c = key.KeyChar;if (IsHexChar(c) && hexStr.Length < 8){hexStr += c.ToString().ToUpper();}}}/// <summary>绘制界面 + 自动计算结果</summary>private static void DrawUI(string hex, int typeIdx, int orderIdx){Console.WriteLine("⫷ ⫷ ⫷ ⫷ ⫷ 32位 16进制转换工具 ⫸ ⫸ ⫸ ⫸ ⫸\n");Console.WriteLine($"≻ 输入16进制(最大8位):{(string.IsNullOrEmpty(hex) ? "待输入" : hex)}\n");Console.WriteLine($"≻ 数据类型:{_typeName[typeIdx]}  【按键 I / F 切换】\n");Console.WriteLine($"≻ 字节字序:{_orderName[orderIdx]}  【← → 方向键切换】\n");Console.WriteLine("⫷ ⫷ ⫷ ⫷ ⫷ ⫷ ⫷ ⫷ ⫷ ⫷ ⫸ ⫸ ⫸ ⫸ ⫸ ⫸ ⫸ ⫸ ⫸ ⫸\n");// 不足8位自动补0string hex8 = hex.PadLeft(8, '0');try{// 原始4字节byte[] rawBytes = HexToBytes(hex8);// 按选中字序重排byte[] outBytes = new byte[4];var map = _orderMap[orderIdx];for (int i = 0; i < 4; i++){outBytes[i] = rawBytes[map[i]];}Console.WriteLine($"≻ 原始字节:{BitConverter.ToString(rawBytes)}\n");Console.WriteLine($"≻ 重组字节:{BitConverter.ToString(outBytes)}\n");Console.WriteLine("⋁ ⋁ ⋁ ⋁ ⋁ ⋁ ⋁ ⋁ ⋁ ⋁ ⋁ ⋁ ⋁ ⋁ ⋁ ⋁ ⋁ ⋁ ⋁ ⋁\n");if (typeIdx == 0){//int intVal = BitConverter.ToInt32(outBytes, 0);int intVal =BinaryPrimitives.ReadInt32BigEndian(outBytes);Console.WriteLine($"≻ 输出整数:{intVal}\n");}else{//float floatVal = BitConverter.ToSingle(outBytes, 0);float floatVal = BinaryPrimitives.ReadSingleBigEndian(outBytes);Console.WriteLine($"≻ 输出浮点:{floatVal:F6} \n");}}catch{Console.WriteLine("⚠️  16进制格式错误");}Console.WriteLine("⋀ ⋀ ⋀ ⋀ ⋀ ⋀ ⋀ ⋀ ⋀ ⋀ ⋀ ⋀ ⋀ ⋀ ⋀ ⋀ ⋀ ⋀ ⋀ ⋀\n");Console.WriteLine("ESC = 清空重输|Backspace = 删除");}/// <summary>合法16进制字符判断</summary>private static bool IsHexChar(char c){return (c >= '0' && c <= '9') ||(c >= 'A' && c <= 'F') ||(c >= 'a' && c <= 'f');}/// <summary>8位16进制转4字节</summary>private static byte[] HexToBytes(string hex8){byte[] res = new byte[4];for (int i = 0; i < 4; i++){string part = hex8.Substring(i * 2, 2);res[i] = Convert.ToByte(part, 16);}return res;}}
}

 

http://www.jsqmd.com/news/713572/

相关文章:

  • NVIDIA发布Nemotron-Cascade-2-30B-A3B:用3B激活参数挑战120B模型的智能密度革命
  • 破解工业流量测量适配痛点:DAP方法论如何助力金属管转子流量计厂家实现精准服务? - 速递信息
  • OBS虚拟背景插件终极指南:3步实现AI智能抠像,告别杂乱背景
  • 第8章: 汽车AI发展趋势
  • ChatGPT非官方API逆向工程:原理、配置与实战应用指南
  • 痘肌敏感肌用什么素颜霜?2026亲测最有效的素颜霜排行榜:自然通透无假面感 - 资讯焦点
  • 2026银川高端系统门窗选购指南:派雅门窗与主流品牌深度横评 - 精选优质企业推荐官
  • Qwen Code 0.12.0 发布:新特性太感人了!
  • 2026年银川门窗定制指南:派雅系统门窗vs行业标杆深度横评 - 精选优质企业推荐官
  • 2026最受用户欢迎dha藻油实测排行推荐:dha藻油哪家好?全人群适配补脑 - 资讯焦点
  • Kubernetes 1.18.6集群部署后,别忘了做这5件事:Dashboard访问、Metrics-Server配置与集群健康检查
  • Zotero 7 升级后插件失效?别慌!手把手教你搞定新版护眼模式和翻译插件(附Zotero6兼容方案)
  • 2026最新高一数学跟不上辅导培训机构推荐!国内权威榜单发布,陕西西安等地优质机构实力上榜 - 十大品牌榜
  • 2026年SUPROME厂家口碑推荐:SUPROME厂怎么选/SUPROME厂家找哪家/SUPROME牌子评测 - 品牌策略师
  • 国内外第一梯队的温度传感器厂家有哪些?内附选型指南 - 仪表人小余
  • 如何快速清理微信单向好友:WechatRealFriends完整检测指南
  • 基于微信小程序的一站式校园缴费平台设计与实现
  • 手把手教你用IPMI远程搞定ESXi 8.0安装,告别实体机前傻等
  • 2026青海防静电地板供货厂家权威TOP5 - 深度智识库
  • 2026年银川高端铝合金门窗深度横评:派雅门窗如何定义深舒适人居标准 - 精选优质企业推荐官
  • 近三十载深耕铸就移民行业标杆,资源矩阵引领身份规划新时代 - 品牌排行榜
  • 观澜社张庆:用“社区茶话会”织就邻里温情网
  • LeetCode Bellman-Ford 算法题解
  • 从混乱网页到整洁笔记:MarkDownload让知识管理变得如此简单
  • 2026年,口碑爆棚的滁州居间金服,究竟哪个才值得你信赖? - GrowthUME
  • 揭秘!黄埔食堂肉菜配送背后的优质服务与新鲜秘诀 - GrowthUME
  • 2026年:如何快速降低论文AIGC率? - 降AI实验室
  • 邮件系统中的区块链技术应用:安全与合规性
  • 库卡机器人自动运行配置
  • 3步搞定Windows风扇噪音:FanControl免费工具完全指南