提问人:nees 提问时间:7/28/2023 最后编辑:Richardnees 更新时间:7/28/2023 访问量:52
将 16 位整数加载到现有字节数组中
Load a 16 bit Integer into an existing byte array
问:
使用 Visual Studio Visual Basic
Microsoft Visual Studio 社区 2019 版本 16.11.27
Microsoft.NET.Framework 版本 4.8.04084
Windows 10 64 位
我创建了一个字节数组,并使用以下方法从二进制文件中读取字节数据:
Dim array() As Byte = File.ReadAllBytes(Filename)
Using memory As MemoryStream = New MemoryStream(array)
Using reader As BinaryReader = New BinaryReader(memory)
现在我从字节数组中读取一个 16 位整数:
Data_Offset = 1024
Data16 = BitConverter.ToUInt16(array, Data_Offset)
现在,我需要修改 Data16 的值,并将其写回同一位置的数组中。
我找不到将新的 Data16 值保存到 array(Data_Offset) 的正确方法
任何帮助都非常感谢
array(1024) = Data16
答:
0赞
dbasnett
7/28/2023
#1
试一试,
Dim MyArray() As Byte = {0, 1, 2, 3, 4, 5, 6, 7}
Dim val As UInt16
Dim Data_Offset As Integer = 2
val = BitConverter.ToUInt16(MyArray, Data_Offset)
val = val + 1023US
Array.Copy(BitConverter.GetBytes(val), 0, MyArray, Data_Offset, 2)
评论
0赞
nees
8/1/2023
什么是1023US?
0赞
dbasnett
8/1/2023
US 是一个类型字符。查看 learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/...
评论