在PowerShell中创建Byte [] [英] Creating Byte[] in PowerShell

查看:75
本文介绍了在PowerShell中创建Byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用COM API的PowerShell代码。传递字节数组时,出现类型不匹配错误。这是我创建数组的方式以及一些类型信息

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

随便看看API,我有发现它正在寻找基本类型为System.Array的Byte []。

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

我要做什么将$ bytes转换为与$ r.data相同的类型。由于某种原因,$ bytes被创建为Object []。如何将其转换为Byte []?

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[]?

推荐答案

将其转换为字节数组:

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

这篇关于在PowerShell中创建Byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆