是否可以在 Powershell 中将字节数组转换为 8 位有符号整数数组?
Is it possible to convert a byte array to a 8-bit signed integer array in Powershell?
我正在尝试在 Powershell 中将十六进制字符串转换为 8 位有符号整数数组。
我正在使用以下函数将十六进制字符串(例如 A591BF86E5D7D9837EE7ACC569C4B59B)转换为字节数组,然后我需要将其转换为 8 位有符号整数数组。
Function GetByteArray {
[cmdletbinding()]
param(
[parameter(Mandatory=$true)]
[String]
$HexString
)
$Bytes = [byte[]]::new($HexString.Length / 2)
For($i=0; $i -lt $HexString.Length; $i+=2){
$Bytes[$i/2] = [convert]::ToByte($HexString.Substring($i, 2), 16)
}
$Bytes
}
使用该函数后,十六进制转换为字节数组,如下所示:
我需要获取无符号字节数组并将 o 转换为 8 位有符号字节数组,如下所示:
这可能吗?如果可以,如何实施?
我试过使用 BitConverter class,但据我所知,它只能转换为 int16。
提前致谢
得到一个[byte[]]
数组([byte]
== System.Byte
,一个unsigned 8位整数类型):
$hexStr = 'A591BF86E5D7D9837EE7ACC569C4B59B' # sample input
[byte[]] -split ($hexStr -replace '..', '0x$& ')
-replace , '..', '0x& '
在每对 (..
) 十六进制数字之前插入 0x
并在之后插入 space,产生字符串 '0xA5 0x91 ... '
应用于结果字符串的 -split
将字符串按 whitespace 拆分为 array 的单个字节表示('0xA5', '0x91', ...
)
将结果转换为 [byte[]]
有助于直接识别此十六进制格式。
- 注意:如果您忘记了转换,您将得到一个 字符串数组 。
得到一个[sbyte[]]
数组 ([sbyte]
== System.SByte
,一个signed 8 位整数),将 直接 转换为 [sbyte[]]
而不是 ; 不要尝试组合演员表:[sbyte[]] [byte[]] (...)
)
更简单的 PowerShell(核心)7.1+ 替代方案,使用 .NET 5+ [System.Convert]::FromHexString()
方法:
$hexStr = 'A591BF86E5D7D9837EE7ACC569C4B59B' # sample input
# Directly returns a [byte[]] array.
# Cast to [sbyte[]] if you need [sbyte] instances.
[System.Convert]::FromHexString($hexStr)
如果你给定一个你的[byte[]]
数组,那么想要转换成[sbyte[]]
,使用以下(可能有更有效的方法):
[byte[]] $bytes = 0x41, 0xFF # sample input; decimal: 65, 255
# -> [sbyte] values of: 65, -1
[sbyte[]] $sBytes = $bytes.ForEach('ToString', 'x') -replace '^', '0x'
应用于您的样本值,以十进制表示:
# Input array of [byte]s.
[byte[]] $bytes = 43, 240, 82, 109, 185, 46, 111, 8, 164, 74, 164, 172
# Convert to an [sbyte] array.
[sbyte[]] $sBytes = $bytes.ForEach('ToString', 'x') -replace '^', '0x'
$sBytes # Output (each signed byte prints on its own line, in decimal form).
输出:
43
-16
82
109
-71
46
111
8
-92
74
-92
-84
我正在尝试在 Powershell 中将十六进制字符串转换为 8 位有符号整数数组。
我正在使用以下函数将十六进制字符串(例如 A591BF86E5D7D9837EE7ACC569C4B59B)转换为字节数组,然后我需要将其转换为 8 位有符号整数数组。
Function GetByteArray {
[cmdletbinding()]
param(
[parameter(Mandatory=$true)]
[String]
$HexString
)
$Bytes = [byte[]]::new($HexString.Length / 2)
For($i=0; $i -lt $HexString.Length; $i+=2){
$Bytes[$i/2] = [convert]::ToByte($HexString.Substring($i, 2), 16)
}
$Bytes
}
使用该函数后,十六进制转换为字节数组,如下所示:
我需要获取无符号字节数组并将 o 转换为 8 位有符号字节数组,如下所示:
这可能吗?如果可以,如何实施?
我试过使用 BitConverter class,但据我所知,它只能转换为 int16。
提前致谢
得到一个[byte[]]
数组([byte]
== System.Byte
,一个unsigned 8位整数类型):
$hexStr = 'A591BF86E5D7D9837EE7ACC569C4B59B' # sample input
[byte[]] -split ($hexStr -replace '..', '0x$& ')
-replace , '..', '0x& '
在每对 (..
) 十六进制数字之前插入0x
并在之后插入 space,产生字符串'0xA5 0x91 ... '
应用于结果字符串的 -split
将字符串按 whitespace 拆分为 array 的单个字节表示('0xA5', '0x91', ...
)将结果转换为
[byte[]]
有助于直接识别此十六进制格式。- 注意:如果您忘记了转换,您将得到一个 字符串数组 。
得到一个
[sbyte[]]
数组 ([sbyte]
==System.SByte
,一个signed 8 位整数),将 直接 转换为[sbyte[]]
而不是 ; 不要尝试组合演员表:)[sbyte[]] [byte[]] (...)
更简单的 PowerShell(核心)7.1+ 替代方案,使用 .NET 5+ [System.Convert]::FromHexString()
方法:
$hexStr = 'A591BF86E5D7D9837EE7ACC569C4B59B' # sample input
# Directly returns a [byte[]] array.
# Cast to [sbyte[]] if you need [sbyte] instances.
[System.Convert]::FromHexString($hexStr)
如果你给定一个你的[byte[]]
数组,那么想要转换成[sbyte[]]
,使用以下(可能有更有效的方法):
[byte[]] $bytes = 0x41, 0xFF # sample input; decimal: 65, 255
# -> [sbyte] values of: 65, -1
[sbyte[]] $sBytes = $bytes.ForEach('ToString', 'x') -replace '^', '0x'
应用于您的样本值,以十进制表示:
# Input array of [byte]s.
[byte[]] $bytes = 43, 240, 82, 109, 185, 46, 111, 8, 164, 74, 164, 172
# Convert to an [sbyte] array.
[sbyte[]] $sBytes = $bytes.ForEach('ToString', 'x') -replace '^', '0x'
$sBytes # Output (each signed byte prints on its own line, in decimal form).
输出:
43
-16
82
109
-71
46
111
8
-92
74
-92
-84