Numbers are stored internally as bytes. An Int32 value, for example, uses four bytes. Sometimes it is required to split up the number into its byte parts, for example, to calculate checksums with the least significant byte.

We have created a quick walk-through that you can also use as a basic number handling tutorial. It illustrates how numbers correspond to bits, how to split numbers to bytes, how to calculate least significant byte (LSB) and most significant byte (MSB), and more:

{
\$Width=80
\$padLeft = [int](\$width / 2) + (\$text.Length / 2)
''
''
}

\$number = 26443007
\$number

Show-Header Display the bits for this number:
\$bits = [Convert]::ToString(\$number,2)
\$bits

# pad the string to its full bit range (32 bits)
\$bitsAligned

Show-Header Display the four byte groups
\$bitsAligned -split '(?<=\G.{8})(?=.)' -join '-'

\$bytes

\$bitbytes = \$bytes | ForEach-Object { [Convert]::ToString(\$_, 2).PadLeft(8,'0')}
\$bitbytes -join '-'

Show-Header Show the Least Significant Byte LSB:
\$bytes[0]

Show-Header Show LSB by turning the 8 bits to the right into a number to verify:
\$bits = [Convert]::ToString(\$number, 2)
# take least significant bits
[Convert]::toByte(\$bits.Substring(\$bits.Length-8),2)

Show-Header Show the Most Significant Byte MSB:
\$bytes[3]

As you can see, there are many ways to get there. One especially clever way is converting a number to an IPAddress. IPAddress objects have a handy GetAddressBytes() method that splits the number easily into its bytes for you.

The result looks like this:

===========================Starting with this number:===========================

26443007

=======================Display the bits for this number:========================

1100100110111110011111111

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

psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU - with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!