gpt4 book ai didi

arrays - 用其他数组替换部分数组的最简单方法? (将二进制数据修补到文件中)

转载 作者:行者123 更新时间:2023-12-03 01:12:51 25 4
gpt4 key购买 nike

使用PS将二进制数据块修补到文件中,这是我想出的最好的方法(为简洁起见,所有内容都经过硬编码):

$bytes = [IO.File]::ReadAllBytes("FILE.DAT")
for ($i = 0; $i -lt 7; $i++) {
$bytes[73 + $i] = (0xCD, 0xCD, 0xCD, 0xA7, 0x91, 0xAB, 0xD2)[$i]
}
[IO.File]::WriteAllBytes("FILE.DAT", $bytes)

它可以工作,但没有一种更简单的方法可以在数组上执行这种操作,例如仅具有一个运算符或调用,而没有一次一次替换元素(此处为字节)的 for循环?

有了PowerShell中的所有高级功能,我希望会有类似 -replace op或C语言中的 memcpy()的版本,或希望通过val而不是ref数组赋值来实现的 $b = $a | foreach {$_}技巧,或者(失败):
$bytes[73..79] = 0xCD, 0xCD, 0xCD, 0xA7, 0x91, 0xAB, 0xD2  # "Array assignment ... failed because assignment to slices is not supported"

第二点,我注意到 @()数组运算符经常在此类示例中使用,尽管不是必需的。有什么理由吗?

最佳答案

您可以使用 Array.CopyTo 方法:

Copies all the elements of the current one-dimensional array to the specified one-dimensional array starting at the specified destination array index.


([byte[]](0xCD, 0xCD, 0xCD, 0xA7, 0x91, 0xAB, 0xD2)).CopyTo($bytes,73)

关于arrays - 用其他数组替换部分数组的最简单方法? (将二进制数据修补到文件中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36238298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com