news 2026/4/28 21:41:43

工业级高性能 32位整数字节序转换工具类

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
工业级高性能 32位整数字节序转换工具类

以下是工业级高性能 32位整数字节序转换工具类,全面覆盖 Modbus 等工业协议中常见的四种字节序:

四种常见 32 位字节序(ABCD 表示法)

  • ABCD:标准 Big-Endian(高字在前,高字节在前)—— 最符合 Modbus 规范
  • BADC:Big-Endian + Byte Swap(字内高低字节交换)
  • CDAB:Little-Endian + Word Swap(字交换)
  • DCBA:标准 Little-Endian(完整反序)
usingSystem;usingSystem.Buffers.Binary;usingSystem.Collections.Generic;usingSystem.Globalization;usingSystem.Runtime.CompilerServices;namespaceIndustrialProtocol{/// <summary>/// 32位整数字节序转换枚举(工业协议常用)/// </summary>publicenumInt32ByteOrder{/// <summary>ABCD - Big-Endian (标准大端,最常见于 Modbus)</summary>ABCD=0,/// <summary>BADC - Big-Endian Byte Swapped</summary>BADC=1,/// <summary>CDAB - Little-Endian Word Swapped</summary>CDAB=2,/// <summary>DCBA - Little-Endian (完整小端)</summary>DCBA=3}/// <summary>/// 工业级 32位整数字节序转换工具类(高性能 + 位运算)/// 支持 string(十六进制) 和 byte[] 两种输入方式/// </summary>publicstaticclassIndustrialInt32ByteOrder{#region核心位运算转换函数/// <summary>/// 对 uint32 执行指定的字节序转换/// </summary>[MethodImpl(MethodImplOptions.AggressiveInlining)]publicstaticuintConvertUInt32(uintvalue,Int32ByteOrderorder){returnorderswitch{Int32ByteOrder.ABCD=>value,// 不变Int32ByteOrder.BADC=>BinaryPrimitives.ReverseEndianness(value),// 字内字节交换Int32ByteOrder.CDAB=>((value&0xFFFF0000U)>>16)|((value&0x0000FFFFU)<<16),// 字交换Int32ByteOrder.DCBA=>BinaryPrimitives.ReverseEndianness(((value&0xFFFF0000U)>>16)|((value&0x0000FFFFU)<<16)),_=>value};}[MethodImpl(MethodImplOptions.AggressiveInlining)]publicstaticintConvertInt32(intvalue,Int32ByteOrderorder){return(int)ConvertUInt32((uint)value,order);}#endregion#region从十六进制字符串解析(每8字符一组)/// <summary>/// 把十六进制字符串解析为 uint32 列表(已完成字节序转换)/// 每8个字符(4字节)为一组,不足时高位补0/// </summary>publicstaticList<uint>ParseToUInt32(stringhexString,Int32ByteOrderbyteOrder=Int32ByteOrder.ABCD){if(string.IsNullOrWhiteSpace(hexString))returnnewList<uint>();hexString=hexString.Replace(" ","").ToUpperInvariant();varresult=newList<uint>(hexString.Length/8+1);for(inti=0;i<hexString.Length;i+=8){stringchunk=hexString.Substring(i,Math.Min(8,hexString.Length-i)).PadLeft(8,'0');if(uint.TryParse(chunk,NumberStyles.HexNumber,CultureInfo.InvariantCulture,outuintbigEndianValue)){uintfinalValue=ConvertUInt32(bigEndianValue,byteOrder);result.Add(finalValue);}else{result.Add(0);}}returnresult;}publicstaticList<int>ParseToInt32(stringhexString,Int32ByteOrderbyteOrder=Int32ByteOrder.ABCD){varuintList=ParseToUInt32(hexString,byteOrder);varintList=newList<int>(uintList.Count);foreach(varuinuintList)intList.Add((int)u);returnintList;}#endregion#region从 byte[] 直接解析(最高性能,推荐真实通信使用)/// <summary>/// 从原始字节数组(Modbus 返回的 byte[])解析 32位整数(已字节序转换)/// </summary>publicstaticuint[]ParseUInt32FromBytes(ReadOnlySpan<byte>data,Int32ByteOrderbyteOrder=Int32ByteOrder.ABCD){if(data.Length<4)returnArray.Empty<uint>();intcount=data.Length/4;varresult=newuint[count];for(inti=0;i<count;i++){intoffset=i*4;// 先组合成 Big-Endian uint(Modbus 单个寄存器为 Big-Endian)uintbigEndian=(uint)((data[offset+0]<<24)|(data[offset+1]<<16)|(data[offset+2]<<8)|(data[offset+3]));result[i]=ConvertUInt32(bigEndian,byteOrder);}returnresult;}#endregion#region辅助方法:返回转换后的十六进制字符串(便于调试)publicstaticList<string>ParseToSwappedHex32(stringhexString,Int32ByteOrderbyteOrder=Int32ByteOrder.ABCD){varvalues=ParseToUInt32(hexString,byteOrder);varhexList=newList<string>(values.Count);foreach(varvinvalues)hexList.Add(v.ToString("X8"));returnhexList;}#endregion#region测试示例publicstaticvoidDemo(){// 测试数据:假设设备发送的原始 hex(两个 32位数)stringinputHex="12345678 9ABCDEF0";// 实际使用时可去掉空格Console.WriteLine("输入 Hex: "+inputHex);foreach(Int32ByteOrderorderinEnum.GetValues(typeof(Int32ByteOrder))){varresults=ParseToUInt32(inputHex.Replace(" ",""),order);Console.WriteLine($"{order,-6}{string.Join(" ",results.Select(v=>v.ToString("X8")))}");}}#endregion}}

使用示例

stringhexData="313239393939450012345678";// 按不同字节序解析varabcd=IndustrialInt32ByteOrder.ParseToUInt32(hexData,Int32ByteOrder.ABCD);varbadc=IndustrialInt32ByteOrder.ParseToUInt32(hexData,Int32ByteOrder.BADC);varcdab=IndustrialInt32ByteOrder.ParseToUInt32(hexData,Int32ByteOrder.CDAB);vardcba=IndustrialInt32ByteOrder.ParseToUInt32(hexData,Int32ByteOrder.DCBA);

性能建议

  • 真实 Modbus/TCP 或串口通信时,优先使用ParseUInt32FromBytes(直接操作byte[]Span<byte>)。
  • .NET 5+强烈推荐使用BinaryPrimitives.ReverseEndianness
  • 对于高频数据,建议把byteOrder配置成枚举或配置文件,避免硬编码。

需要我继续扩展32位浮点数(Float / Real)的四种字节序转换版本吗?或者增加64位(long / double)支持?随时告诉我具体需求。

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

拆解对比:ABLIC S-8254A与TI BQ系列,3/4串锂电池保护方案怎么选?

ABLIC S-8254A与TI BQ系列锂电池保护IC深度对比&#xff1a;3/4串方案选型指南 在电动工具、便携储能设备和高性能无人机等产品的设计中&#xff0c;锂电池保护电路的选择往往直接关系到产品的安全性和可靠性。面对市场上众多的保护IC方案&#xff0c;工程师们常常陷入选择困境…

作者头像 李华
网站建设 2026/4/28 21:28:20

Python 自动化爬取网易云音乐歌手歌词实战教程

网易云音乐歌词数据分散于多页面&#xff0c;手动复制效率低下、易出现内容遗漏&#xff0c;且无法满足批量采集需求。自动化爬取面临两大核心技术难点&#xff1a;其一&#xff0c;歌词数据通过 AJAX 异步动态加载&#xff0c;原生<font style"color:rgb(0, 0, 0);bac…

作者头像 李华
网站建设 2026/4/28 21:25:22

3分钟搭建完整KIMI AI免费API:解锁智能对话接口的终极解决方案

3分钟搭建完整KIMI AI免费API&#xff1a;解锁智能对话接口的终极解决方案 【免费下载链接】kimi-free-api &#x1f680; KIMI AI 长文本大模型逆向API【特长&#xff1a;长文本解读整理】&#xff0c;支持高速流式输出、智能体对话、联网搜索、探索版、K1思考模型、长文档解读…

作者头像 李华
网站建设 2026/4/28 21:20:25

机器学习大师课 第 1 课:什么是机器学习?写出你的第一个 AI 程序

课程承诺&#xff1a;每节课只讲 1 个核心概念、1 个核心思想、1 段可运行代码。学完立刻能用&#xff0c;绝不讲听不懂的废话。本节课目标&#xff1a;彻底搞懂机器学习和普通编程的本质区别&#xff0c;亲手写出人生第一个机器学习程序&#xff0c;5 分钟内看到 AI 预测结果。…

作者头像 李华