Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Convert Decimal to Binary

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 02-28-2008   #1 (permalink)
Cerox
Guest


 

Convert Decimal to Binary

Hi,

Is there a possibility with Windows Power Shell to convert from decimal to
binary?

Like the "decbin" Function in PHP.

My System SpecsSystem Spec
Old 02-28-2008   #2 (permalink)
Cerox
Guest


 

Convert from decimal to binary

Hi,

Is there a possibility in Windows PowerShell to convert from decimal to
binary?

Like the "decbin" function in PHP...
My System SpecsSystem Spec
Old 02-28-2008   #3 (permalink)
Shay Levi
Guest


 

Re: Convert Decimal to Binary



Try:

# convert 192 to binary form
PS > [Convert]::ToString(192,2).PadLeft(8,"0")
11000000



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Hi,
>
> Is there a possibility with Windows Power Shell to convert from
> decimal to binary?
>
> Like the "decbin" Function in PHP.
>

My System SpecsSystem Spec
Old 02-28-2008   #4 (permalink)
Keith Hill [MVP]
Guest


 

Re: Convert from decimal to binary

"Cerox" <Cerox@xxxxxx> wrote in message
news:63C444F0-B3DD-4BF8-9907-C8620A243E78@xxxxxx
Quote:

> Hi,
>
> Is there a possibility in Windows PowerShell to convert from decimal to
> binary?
>
> Like the "decbin" function in PHP...
It's a pretty easy function to write:

function decbin([int]$num) {
$i = 0
$bin = ""
do {
$bin = "$($num % 2)$bin"
$num = [Math]::Truncate($num / 2)
if (++$i % 4 -eq 0) {$bin = " $bin"}
} while ($num -gt 0)
$bin
}

The one tricky part is using the Truncate method. You would think that you
could just use integer division within PowerShell but noooo. It uses
banker's algorithm rounding so odd nums round up and even's round down.
Considering that not many bankers will ever use PowerShell I have no idea
why they chose that form of rounding or rounding at all! It seems like to
me that integer division should just truncate like it does in C.

--
Keith

My System SpecsSystem Spec
Old 02-28-2008   #5 (permalink)
Keith Hill [MVP]
Guest


 

Re: Convert Decimal to Binary

"Shay Levi" <no@xxxxxx> wrote in message
news:8766a944211d48ca48372ab37582@xxxxxx
Quote:

>
>
> Try:
>
> # convert 192 to binary form
> PS > [Convert]::ToString(192,2).PadLeft(8,"0")
> 11000000
>
C'mon that takes all the fun out of writing it. :-)

--
Keith

My System SpecsSystem Spec
Old 02-28-2008   #6 (permalink)
Shay Levi
Guest


 

Re: Convert from decimal to binary

Added to my library



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> "Cerox" <Cerox@xxxxxx> wrote in message
> news:63C444F0-B3DD-4BF8-9907-C8620A243E78@xxxxxx
>
Quote:

>> Hi,
>>
>> Is there a possibility in Windows PowerShell to convert from decimal
>> to binary?
>>
>> Like the "decbin" function in PHP...
>>
> It's a pretty easy function to write:
>
> function decbin([int]$num) {
> $i = 0
> $bin = ""
> do {
> $bin = "$($num % 2)$bin"
> $num = [Math]::Truncate($num / 2)
> if (++$i % 4 -eq 0) {$bin = " $bin"}
> } while ($num -gt 0)
> $bin
> }
> The one tricky part is using the Truncate method. You would think
> that you could just use integer division within PowerShell but noooo.
> It uses banker's algorithm rounding so odd nums round up and even's
> round down. Considering that not many bankers will ever use PowerShell
> I have no idea why they chose that form of rounding or rounding at
> all! It seems like to me that integer division should just truncate
> like it does in C.
>
> --
> Keith

My System SpecsSystem Spec
Old 02-28-2008   #7 (permalink)
Oisin (x0n) Grehan [MVP]
Guest


 

Re: Convert from decimal to binary

On Feb 28, 12:55*pm, "Keith Hill [MVP]"
<r_keith_h...@xxxxxx_spam_I> wrote:
Quote:

> "Cerox" <Ce...@xxxxxx> wrote in message
>
> news:63C444F0-B3DD-4BF8-9907-C8620A243E78@xxxxxx
>
Quote:

> > Hi,
>
Quote:

> > Is there a possibility in Windows PowerShell to convert from decimal to
> > binary?
>
Quote:

> > Like the "decbin" function in PHP...
>
> It's a pretty easy function to write:
>
> function decbin([int]$num) {
> * * $i = 0
> * * $bin = ""
> * * do {
> * * * * $bin = "$($num % 2)$bin"
> * * * * $num = [Math]::Truncate($num / 2)
> * * * * if (++$i % 4 -eq 0) {$bin = " $bin"}
> * * } while ($num -gt 0)
> * * $bin
>
> }
>
> The one tricky part is using the Truncate method. *You would think that you
> could just use integer division within PowerShell but noooo. *It uses
> banker's algorithm rounding so odd nums round up and even's round down.
> Considering that not many bankers will ever use PowerShell I have no idea
> why they chose that form of rounding or rounding at all! *It seems like to
> me that integer division should just truncate like it does in C.
>
> --
> Keith
You could also use:

ps> [convert]::ToString(16, 2)
10000

and the reverse conversion is done with:

ps> [convert]::toint32("10000", 2)
16

For padding with zeros, use:

ps> [convert]::tostring(16, 2).padleft(8, "0")
00010000

Admittedly ToString() is not the first place i'd look for this
either ;-)

- Oisin
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert older binary *.reg to Vista *.reg? Jrz Vista General 6 12-14-2007 12:58 PM
Stock Gadget decimal places Qualnhick Vista General 0 11-24-2007 10:04 AM
PInvoke and Decimal to Binary Conversion Joris van Lier PowerShell 2 05-04-2007 05:48 PM
How to embed manifest in TCL binary? - mt.exe corrupting my binary Kshitij Vista General 0 02-14-2007 04:42 PM
Convert Byte to Binary perimere PowerShell 2 02-05-2007 10:08 AM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008