更新时间:2023-11-15 20:07:04
获得 [byte []]
数组( [byte]
== System.Byte
, unsigned 8位整数类型):
To get a [byte[]]
array ([byte]
== System.Byte
, an unsigned 8-bit integer type):
$hexStr = 'A591BF86E5D7D9837EE7ACC569C4B59B' # sample input
[byte[]] ($hexStr -split '(.{2})' -ne '' -replace '^', '0X')
-split'(。{2})'
用2个字符分割输入字符串序列,并用(...)
括起来会导致这些序列包含在返回的令牌中; -ne''
然后清除 empty 令牌(从技术上讲是实际的数据令牌)。
-split '(.{2})'
splits the input string by 2-character sequences, and enclosure in (...)
causes these sequences to be included in the returned tokens; -ne ''
then weeds out the empty tokens (which are technically the actual data tokens).
-replace,'^','0X'
将前缀 0X
放在每个结果之前2个十六进制数字的字符串,产生数组'0XA5','0X91',...
-replace , '^', '0X'
places prefix 0X
before each resulting 2-hex-digit string, yielding array '0XA5', '0X91', ...
将结果投射到 [byte []]
有助于直接识别此十六进制格式。
casting the result to [byte[]]
helpfully recognizes this hex format directly.
要获取 [sbyte []]
数组( [sbyte]
== 系统.SByte
,一个 signed 8位整数),直接将投射到 [sbyte []]
代替; not 尝试组合强制转换: ) [sbyte []] [byte []](...)
To get an [sbyte[]]
array ([sbyte]
== System.SByte
, a signed 8-bit integer), cast directly to [sbyte[]]
instead; do not try to combine the casts: )[sbyte[]] [byte[]] (...)
如果您 已获 然后要转换为 [sbyte []]
的 [byte []]
数组> ,请使用以下方法(可能有更有效的方法):
If you're given a [byte[]]
array that you then want to convert to [sbyte[]]
, use the following (there may be more efficient approaches):
[byte[]] $bytes = 0x41, 0xFF # sample input; decimal: 65, 255
# -> [sbyte] values of: 65, -1
[sbyte[]] $sBytes = ($bytes.ForEach('ToString', 'X') -replace '^', '0X')
应用于样本值,以十进制表示:
Applied to your sample values, in decimal notation:
# 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