Calculating Most and Least Significant Byte

by Mar 6, 2019

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:

function Show-Header([Parameter(ValueFromRemainingArguments)][string]$Text)
  $padLeft = [int]($width / 2) + ($text.Length / 2)
  $text.PadLeft($padLeft, "=").PadRight($Width, "=")

Show-Header Starting with this number:
$number = 26443007

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

Show-Header Add missing leading zeroes:
# pad the string to its full bit range (32 bits)
$bitsAligned = $bits.PadLeft(32, '0') 

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

Show-Header Get the bytes by conversion to IPAddress object:
$bytes = ([Net.IPAddress]$number).GetAddressBytes()

Show-Header Display the bits for the IPAddress bytes:
$bitbytes = $bytes | ForEach-Object { [Convert]::ToString($_, 2).PadLeft(8,'0')}
$bitbytes -join '-'

Show-Header Show the Least Significant Byte LSB:

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

Show-Header Show the Most Significant Byte MSB:

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:===========================


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


===========================Add missing leading Zeroes:==========================


==========================Display the four byte groups==========================


================Get the bytes by conversion to IPAddress object:================


===================Display the bits for the IPAddress bytes:====================


======================Show the Least Significant Byte LSB:======================


======Show LSB by turning the 8 bits to the right into a number to verify:======


=======================Show the Most Significant Byte MSB:======================

1 – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit 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, 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!

Twitter This Tip! ReTweet this Tip!