当前位置: 首页> 文旅> 艺术 > 用C# 代码调整16位整数大小端的4种方法

用C# 代码调整16位整数大小端的4种方法

时间:2025/7/14 1:30:55来源:https://blog.csdn.net/helldoger/article/details/140349854 浏览次数:0次

四种方法:

short BLC(short s)
{byte high = (byte)((s - s % 256) / 256);  //数字减去 低8位, 得到的数字再除以256得到高8位byte low = (byte)(s % 256);  //数字对256取余数, 得到低8位byte[] change1 = { high, low };return BitConverter.ToInt16(change1);
}
short BLC2(short s)
{byte[] bytes = BitConverter.GetBytes(s);Array.Reverse(bytes);return BitConverter.ToInt16(bytes, 0);
}
short BLC3(short s)
{byte highByte = (byte)(s >> 8); // 右移8位获取高字节byte lowByte = (byte)(s & 0xFF); // 与16位,高8位是0, 低8位是11111111byte[] bytes = { highByte, lowByte };return BitConverter.ToInt16(bytes);
}
short BLC4(short s)
{byte[] bytes = BitConverter.GetBytes(s);return BitConverter.ToInt16([bytes[1], bytes[0]]);
}

结果:
在这里插入图片描述

关键字:用C# 代码调整16位整数大小端的4种方法

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: