This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
Sometimes you need read or set a options property that is a Byte.
then depending on the bits enabled in the byte options are On or Off.
but if you get the byte you will get a Number from 0 to 255.
the script below will create the Function show-Byte, that will show you a table of the Bits set, in the Byte Given.
So that you can easy see what properties are set.
You can use it like this :
MSH>show-byte 23
─────────────
bn = Bit Number
bv = Bit Value
Wght = Bit Weight
value = Value
─────────────
bn bv Wght value
┌─┐┌─┐┌───┐┌───┐
│7││0││128││ 0│
└─┘└─┘└───┘└───┘
┌─┐┌─┐┌───┐┌───┐
│6││0││ 64││ 0│
└─┘└─┘└───┘└───┘
┌─┐┌─┐┌───┐┌───┐
│5││0││ 32││ 0│
└─┘└─┘└───┘└───┘
┌─┐┌─┐┌───┐┌───┐
│4││1││ 16││ 16│
└─┘└─┘└───┘└───┘
┌─┐┌─┐┌───┐┌───┐
│3││0││ 8││ 0│
└─┘└─┘└───┘└───┘
┌─┐┌─┐┌───┐┌───┐
│2││1││ 4││ 4│
└─┘└─┘└───┘└───┘
┌─┐┌─┐┌───┐┌───┐
│1││1││ 2││ 2│
└─┘└─┘└───┘└───┘
┌─┐┌─┐┌───┐┌───┐
│0││1││ 1││ 1│
└─┘└─┘└───┘└───┘
───
Total: 23
in this example you can see that a value of 23 means that option 0,1,2 and 4, are enabled.
enjoy,
gr /\/\o\/\/
function Show-Byte ([byte]$byte) {
$bits = @(1);$(for ($i = 1 ; $i -lt 8; $i++) {$bits += [int][math]::pow(2,$i)})
$bitArray = @()
$ValArray = @()
$bits | sort -desc | foreach {
if (($byte / $_) -ge 1){
$ValArray += $_
$bitArray += 1
$byte -= $_
}Else{
$bitArray += 0
$ValArray += 0
}
}
# Generate Byte Table
"─────────────"
"bn = Bit Number"
"bv = Bit Value"
"Wght = Bit Weight"
"value = Value"
"─────────────"
"bn bv Wght value"
for ($i = 7 ; $i -ge 0 ;$i--){
$ShowArray = @"
┌─┐┌─┐┌───┐┌───┐
│$i││$($bitArray[(7 - $i)])││$(($bits[$i]).tostring().padleft(3))││$(($ValArray[(7 - $i)]).tostring().padleft(3))│
└─┘└─┘└───┘└───┘
"@
$ShowArray
}
" ───"
"Total: $((($ValArray | measure-object -sum).sum).tostring().padleft(3))"
}