PowerShell 技能连载 - 计算最高和最低有效字节
数字在内部是以字节的形式存储的。例如一个 Int32
数值使用 4 个字节。有些时候需要将数字分割为字节,例如要以 TODO least significiant 字节顺序计算校验和。
我们创建了一个快速的全览,您也可以将它当作一个基本的数字处理教程。它演示了数字如何对应到字节,并且如何计算最低有效字节 (LSB) 和最高有效字节 (MSB),以及其它:
1 | function Show-Header([Parameter(ValueFromRemainingArguments)][string]$Text) |
如您所见,有许多方法可以实现。一个特别聪明的办法是将数字转换为一个 IPAddress
。IPAddress
对象有一个好用的 GetAddressBytes()
方法,可以将数字轻松地分割为字节。
结果看起来类似这样:
===========================Starting with this number:===========================
26443007
=======================Display the bits for this number:========================
1100100110111110011111111
===========================Add missing leading Zeroes:==========================
00000001100100110111110011111111
==========================Display the four byte groups==========================
00000001-10010011-01111100-11111111
================Get the bytes by conversion to IPAddress object:================
255
124
147
1
===================Display the bits for the IPAddress bytes:====================
11111111-01111100-10010011-00000001
======================Show the Least Significant Byte LSB:======================
255
======Show LSB by turning the 8 bits to the right into a number to verify:======
255
=======================Show the Most Significant Byte MSB:======================
1
PowerShell 技能连载 - 计算最高和最低有效字节
http://blog.vichamp.com/2019/03/06/calculating-most-and-least-significant-byte/