Tuesday, 10 February 2009

Preventing boredom with numbers (and Powershell)..part 2

I spent a fair proportion of today stuck in a meeting (it was one of my favourite kind, lots of flip chart paper & post-it notes, and no coffee....) Anyway, there were a couple of occasions where my concentration lapsed, in fact at one point I had to 'fess up to having not been paying attention as I was asked a question and it was obvious that my thoughts were elsewhere and I had almost no idea what the topic of discussion was. It was during one of these particularly post-it note intensive sessions that for seemingly no reason I wondered how easy it would be to get the Powershell one-liner that listed a Fibonacci sequence of number to display the binary or hex equivalents. The asnwer, it turns out, is 'very easy' as you can simply leverage the power of the .NET framework from within Powershell eg: to convert 734 to binary (base2) you'd simply run:
[Convert]::ToString(734, 2)

or to hex:
[Convert]::ToString(734, 16)

Adding the convert functionality to my one-liner stopped it being just that (a one-liner!) and it 's now:
$1=0;$2=1;$3=$1+$2
do{
$3hex=[convert]::ToString($3, 16)
$3bin=[convert]::ToString($3, 2)
$output="$3`t`t$3hex`t`t$3bin"
$output
$1=$2;$2=$3;$3=$1+$2}
while ($3 -lt 10000000)

So thanks to the power of excessive post-it note usage I've tried something new with Powershell today...

No comments:

Post a Comment