Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 正在PowerShell中创建字节[]_Arrays_Powershell_Byte - Fatal编程技术网

Arrays 正在PowerShell中创建字节[]

Arrays 正在PowerShell中创建字节[],arrays,powershell,byte,Arrays,Powershell,Byte,我有一些使用COM API的PowerShell代码。当我传入字节数组时,得到一个类型不匹配错误。下面是我如何创建数组的,以及一些类型信息 PS C:\> $bytes = Get-Content $file -Encoding byte PS C:\> $bytes.GetType() IsPublic IsSerial Name BaseType -------- -------- ----

我有一些使用COM API的PowerShell代码。当我传入字节数组时,得到一个类型不匹配错误。下面是我如何创建数组的,以及一些类型信息

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
翻开API,我发现它正在寻找一个基类型为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

我试图做的是将$bytes转换为与$r.data相同的类型。出于某种原因,$bytes被创建为对象[]。如何将其转换为字节[]?

将其转换为字节数组:

[byte[]]$bytes = Get-Content $file -Encoding byte

这个答案是针对没有背景的问题。我添加它是因为搜索结果

[System.Byte[]]::CreateInstance([System.Byte],<Length>)
[System.Byte[]::CreateInstance([System.Byte],)
在PS 5.1中,这:

[System.Byte[]::CreateInstance()

不适合我。于是我做了:

新对象字节[]4

这导致了一个空字节[4]:

0
0
0
0

可能还有更多的方法,但我能想到的是:

直接阵列初始化:

[字节[]]$b=1,2,3,4,5
$b=[字节]1,2,3,4,5
$b=@([字节]1,2,3,4,5)
$b=[字节]1..5
创建零初始化数组

$b=[System.Array]::CreateInstance([byte],5)
$b=[字节[]]::新的(5)#Powershell v5+
$b=新对象字节[]5
$b=新对象-类型名字节[]-Args 5
如果您想要一个
字节[]
数组(二维数组)

#5乘5
[byte[,]$b=[System.Array]::CreateInstance([byte],@(5,5))#@()对于2D和3D是可选的
[字节[,]$b=[字节[,]]::新的(5,5)
此外:

#三维
[字节[,]]$b=[字节[,]::新的(5,5,5)
[byte[,]$b=[System.Array]::CreateInstance([byte],5,5,5)

FWIW如果要将任意字符串编码为byte[]数组:

$foo = "This is a string"
[byte[]]$bar = $foo.ToCharArray()

谢谢你!为我节省了一大堆额外的谷歌资源。这不起作用——PowerShell抱怨您无法将
System.Object[]
转换为
System.Byte[]
@brainslaugs83。我在发布之前测试了它,如果您不忽略
-Encoding Byte
参数,它确实起作用。如果它对您不起作用,您可能希望发布一个新问题,其中包含您的PowerShell版本和完整的错误消息。我特意用非详细的表单回答了这个问题。有人想让其他人知道详细格式是
newobject-TypeName byte[]-ArgumentList 4
Try[byte[]]::New(4)这只适用于ASCII。一旦您有一个不适合8位的
char
,通过这种方式将其转换为
byte
会丢失信息。
char
在.NET中是16位的。