Delphi 内存复制与记录到另一个记录
我的逻辑有问题。我不知道如何在 Delphi 中将记录复制到另一条记录。
TypeA = record
value1 : word;
value2 : word;
value3 : word;
end;
TypeB = record
b1 : byte;
b2 : byte;
end;
我有两条记录 TypeA 和 TypeB。例如,我发现TypeA记录中的数据属于TypeB记录。 注意:TypeA 的数据长度较长。
问题:如何复制 TypeA 内存并将其放入 TypeB 记录中?
CopyMemory(@TypeA, @TypeB, Length(TypeB))
当我尝试 CopyMemory 并收到错误(不兼容)类型)。
PS:我不想像下面这样复制或分配它。
TypeB.b1 := TypeA.value1 && $FF;
TypeA 和 TypeB 只是示例记录。大多数情况下,记录TypeA和TypeB可能包含多个记录,并且它 分配表单 TypeA 并分配给 TypeB 记录会更困难。
提前致谢
----补充问题:
有没有办法将Delphi记录复制到字节数组,如何?如果有,
- TypeA 记录到 Byte Array
- Byte Array 到 Type B
这个逻辑行得通吗?
I am having problem with the logic. I have no idea how to copy record to another record in Delphi.
TypeA = record
value1 : word;
value2 : word;
value3 : word;
end;
TypeB = record
b1 : byte;
b2 : byte;
end;
I have my two records TypeA and TypeB. For example, I found out that data in TypeA record is belong to TypeB records. Note: TypeA has longer Data Length.
Question: How do i copy TypeA memory and place it in TypeB record?
CopyMemory(@TypeA, @TypeB, Length(TypeB))
When i tried CopyMemory and got an error (Incompaible types).
PS: I don't want to copy or assign it like below.
TypeB.b1 := TypeA.value1 && $FF;
TypeA and TypeB are just example records. Most of thecases, record TypeA and TypeB may contain multple records and it
will be harder to allocate form TypeA and assign to TypeB record.
Thanks in advance
----Addition question:
Is there a way to copy Delphi record to Byte array and how? If there is,
- TypeA record to Byte Array
- Byte Array to Type B
Will this logic work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果
a
的类型为TypeA
,而b
的类型为TypeB
。if
a
is of typeTypeA
andb
is of typeTypeB
.变体记录:
Variant records:
或(带数学单位)
or (with Math unit)
将A记录复制到B记录有一个好方法——运算符重载;在这种情况下,您可以简单地重载隐式和显式操作并编写类似
b := a
的代码:您可以使用 TBytesStream 将记录复制到字节数组:
或者简单地将字节数组的大小设置为
sizeof (A)
,然后复制内存
to copy A record to B record there is a good way - operator overloading; in this case you can simply overload Implicit and Explicit operations and write code like
b := a
:you can use TBytesStream to copy record to byte array:
or simply set size of byte array to
sizeof(A)
and thencopyMemory
所有其他答案都是直接复制内存的变体,但我认为这是错误的方法。它是低级的,并且您使用的是高级语言,很容易犯错误,而且它如果您的记录包含托管数据类型,则存在风险。
如果您尝试获取记录的一个元素的前两个字节,请尝试使用变体记录(在 C 中为联合)。它是记录的一部分,可以通过多个地址进行寻址。方式,在这里你想将其作为一个字或一系列字节来寻址。
尝试这样的事情(未经测试,只是在 SO 编辑器中输入):
然后对于代码:
这比直接复制内存给你带来了什么更好?
case ... of
' 语法很愚蠢,但是这是另一个讨论...:))一些注意事项:
Word
更改为SmallInt
,因为我找到了Word
令人困惑 - 它实际上不是机器的“字长”, 它是 16 位。打包记录
”,这是理由这是...不确定。All the other answers are variations of copying memory directly, but I think that is the wrong approach. It's low-level and you're using a high-level language, it's easy to make mistakes, and it is risky if your records contain managed data types.
If you're trying to get the first two bytes of one element of a record, try using a variant record (in C, a union.) It is a section of a record that can be addressed several ways, and here you want to address it either as a word or series of bytes.
Try something like this (untested, just typed in the SO editor):
Then for code:
What does this give you that's better than directly copying memory?
case ... of
' syntax for this is silly, but that's another discussion... :))Some notes:
Word
toSmallInt
since I findWord
confusing - it's not actually the 'word size' of the machine, it's 16 bits.packed record
', the justification for this is... iffy.您可以通过以下方式省略 CopyMemory()(Windows 单元)的使用:
You can omit CopyMemory() (Windows unit) use by:
如果您只想将字节从一个位置复制到另一个位置,请使用 MOVE 而不是 CopyMemory。
要获取记录的大小,请使用 SizeOf 而不是 Length。
Use MOVE instead of CopyMemory if you want to simply copy the bytes from one place to another.
And to get the size of a record use SizeOf instead of Length.