Discussion:
Creating a byte array
(too old to reply)
Thomas Lee
2007-03-04 15:39:23 UTC
Permalink
I'm trying to illustrate the use of the .Net random number class
(system.random). It contains a method "NextBytes" with the Definition of
System.Void NextBytes(Byte[] buffer) but I can't work out how to call it
to generate a random string.

How do you create a byte array for use with this class using PowerShell?
In C# it's easy:


Random randObj = new Random();
Byte[] randArray = new Byte[20];randObj.NextBytes(randArray);

But I can not replicate this code in PowerShell. I've tried obvious:

PSH [D:\foo]: $str=new-object [byte[]]
New-Object : Cannot find type [[byte[]]]: make sure the assembly
containing this type is loaded.
At line:1 char:16
+ $str=new-object <<<< [byte[]]

PSH [D:\foo]: $str=new-object byte[]
New-Object : Constructor not found. Cannot find an appropriate
constructor for type byte[].
At line:1 char:16
+ $str=new-object <<<< byte[]


I know I'm probably missing some thing pretty vital in terms of
transferring my limited C# knowledge to PowerShell.

Any Clues??

This is driving me nuts!!

Thomas
--
Thomas Lee
***@gmail.com
MVP - Admin Frameworks and Security
Keith Hill
2007-03-04 18:03:48 UTC
Permalink
Post by Thomas Lee
I'm trying to illustrate the use of the .Net random number class
(system.random). It contains a method "NextBytes" with the Definition of
System.Void NextBytes(Byte[] buffer) but I can't work out how to call it
to generate a random string.
How do you create a byte array for use with this class using PowerShell?
$randArray = New-Object byte[] 20

or

$b = [byte[]](1..255)

Note that the first example isn't exactly well documented. I picked it up
from an earlier post that Bruce answered.

--
Keith
Thomas Lee
2007-03-04 19:28:26 UTC
Permalink
Post by Keith Hill
Post by Thomas Lee
I'm trying to illustrate the use of the .Net random number class
(system.random). It contains a method "NextBytes" with the Definition
of System.Void NextBytes(Byte[] buffer) but I can't work out how to
call it to generate a random string.
How do you create a byte array for use with this class using PowerShell?
$randArray = New-Object byte[] 20
THANKS!
Post by Keith Hill
or
$b = [byte[]](1..255)
Note that the first example isn't exactly well documented. I picked it
up from an earlier post that Bruce answered.
This is something that has long confounded me. As it turns out, I'm
working on an article and a blog post on this point and am most grateful
for the clues.

On a somewhat separate tack:

As a non-programmer, I have always found converting the C# dialect to
PowerShell hard to understand and felt I was missing something.

Let's look at the C# definition of the random object:

Random randObj = new Random();

I can prettily easily see that variable explicitly of type Random,
called randObj, is being assigned the value of a new Random object using
a default constructor.

So is the direct conversion of this C# construct into PowerShell just
always as simple as:

1. Assigning a variable to a call to the New-Object cmdlet (vs C#s use
of the new keyword)
2. Dropping the initial cast (i.e. Random)
3. Adding the type directly after the call to New-Object - Random in
this case, but the one to the left of the C# variable name in the C#
code in general
4. Following that, any constructor parameters done normal psh style
(i.e. space delimited, etc?

If so, then this means this C# declaration

Byte[] randArray = new Byte[20]

would be

$randArray = new-object byte[] 20

Which it is! I think I get this at long last.


Thanks.

Thomas
--
Thomas Lee
***@gmail.com
MVP - Admin Frameworks and Security
Desmond Lee
2007-03-06 09:30:19 UTC
Permalink
Post by Keith Hill
$randArray = New-Object byte[] 20
This syntax would sit well for those familiar with Java :-)
--
http://www.leedesmond.com/
Post by Keith Hill
Post by Thomas Lee
I'm trying to illustrate the use of the .Net random number class
(system.random). It contains a method "NextBytes" with the Definition of
System.Void NextBytes(Byte[] buffer) but I can't work out how to call it
to generate a random string.
How do you create a byte array for use with this class using PowerShell?
$randArray = New-Object byte[] 20
or
$b = [byte[]](1..255)
Note that the first example isn't exactly well documented. I picked it up
from an earlier post that Bruce answered.
--
Keith
Rob Kenny
2007-03-04 18:15:14 UTC
Permalink
Post by Thomas Lee
I'm trying to illustrate the use of the .Net random number class
(system.random). It contains a method "NextBytes" with the Definition of
System.Void NextBytes(Byte[] buffer) but I can't work out how to call it
to generate a random string.
How do you create a byte array for use with this class using PowerShell?
Random randObj = new Random();
Byte[] randArray = new Byte[20];randObj.NextBytes(randArray);
PSH [D:\foo]: $str=new-object [byte[]]
New-Object : Cannot find type [[byte[]]]: make sure the assembly
containing this type is loaded.
At line:1 char:16
+ $str=new-object <<<< [byte[]]
PSH [D:\foo]: $str=new-object byte[]
New-Object : Constructor not found. Cannot find an appropriate
constructor for type byte[].
At line:1 char:16
+ $str=new-object <<<< byte[]
I know I'm probably missing some thing pretty vital in terms of
transferring my limited C# knowledge to PowerShell.
Any Clues??
This is driving me nuts!!
Thomas
$str = New- Object Byte

Rob Kenny
Thomas Lee
2007-03-04 18:38:24 UTC
Permalink
Post by Rob Kenny
$str = New- Object Byte
Or course you meant

$str=New-Object byte

:-_

Thanks - much appreciated.

For what I was trying to achieve, Keith's advice which came in first
helped. I needed a fixed length buffer.

Thomas
--
Thomas Lee
***@gmail.com
MVP - Admin Frameworks and Security
Loading...