32

I have some PowerShell code that is using a COM API. I am getting a Type Mismatch error when I pass in a byte array. Here is how I am creating the array, as well as some type information

PS C:\> $bytes = Get-Content $file -Encoding byte
PS C:\> $bytes.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array


PS C:\> $bytes[0].GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Byte                                     System.ValueType

Poking around with the API, I have found that it is looking for a Byte[] with a base type of System.Array.

PS C:\> $r.data.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Byte[]                                   System.Array

PS C:\> $r.data[0].gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Byte                                     System.ValueType

What I am trying to do is convert $bytes into the same type as $r.data. For some reason, $bytes is getting created as an Object[]. How can I cast it to a Byte[]?

5 Answers 5

45

This answer is for the question with no context. I'm adding it because of search results.

[System.Byte[]]::CreateInstance([System.Byte],<Length>)
0
26

There are probably even more ways, but these are the ones I can think of:

Direct array initialization:

[byte[]] $b = 1,2,3,4,5
$b = [byte]1,2,3,4,5
$b = @([byte]1,2,3,4,5)
$b = [byte]1..5

Create a zero-initialized array

$b = [System.Array]::CreateInstance([byte],5)
$b = [byte[]]::new(5)        # Powershell v5+
$b = New-Object byte[] 5
$b = New-Object -TypeName byte[] -Args 5

And if you ever want an array of byte[] (2-D array)

# 5 by 5
[byte[,]] $b = [System.Array]::CreateInstance([byte],@(5,5)) # @() optional for 2D and 3D
[byte[,]] $b = [byte[,]]::new(5,5)

Additionally:

# 3-D
[byte[,,]] $b = [byte[,,]]::new(5,5,5)
[byte[,]] $b = [System.Array]::CreateInstance([byte],5,5,5)
1
  • You should double-check your options and verify which one creates an array of bytes and which one creates an array of objects.
    – Carsten
    Mar 3, 2022 at 12:45
25

In PS 5.1, this:

[System.Byte[]]::CreateInstance(<Length>)

didn't work for me. So instead I did:

new-object byte[] 4

which resulted in an empty byte[4]:

0
0
0
0
2
  • 1
    I deliberately answered this with the non-verbose form. Someone wants others to know that the verbose form is New-Object -TypeName byte[] -ArgumentList 4
    – Andrew
    Sep 7, 2018 at 12:54
  • 2
    Try [byte[]]::new(4)
    – Brain2000
    Dec 4, 2018 at 22:02
21

Cast it to a byte array:

[byte[]]$bytes = Get-Content $file -Encoding byte
2
  • This doesn't work -- PowerShell complains that you can't convert System.Object[] to System.Byte[]. Jun 28, 2018 at 2:49
  • @BrainSlugs83 I tested this before posting and it does work, provided you don't omit the -Encoding Byte argument. If it doesn't work for you you may want to post a new question with a minimal reproducible example including your PowerShell version and the full error message. Jun 28, 2018 at 6:55
0

FWIW if you want to encode just any arbitrary string as a byte[] array:

$foo = "This is a string"
[byte[]]$bar = $foo.ToCharArray()
1
  • This will work for ASCII only. Once you have a char that does not fit to 8 bits, you lose information by casting it to byte this way. The char is 16-bit in .NET. Sep 24, 2020 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.