string Delphi,将字符串复制到字节数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1824373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delphi, Copy string to Byte array
提问by Christopher Chase
what i have works, but im looking if there is a faster way to copy a string into a pByteArray
我有什么工作,但我在寻找是否有更快的方法将字符串复制到 pByteArray
from sysutils
来自 sysutils
PByteArray = ^TByteArray;
TByteArray = array[0..32767] of Byte;
assume aand sare setup correctly
假设a和s设置正确
a: pByteArray;
s: string;
is there a fast way to do this, ie something like copy
有没有一种快速的方法可以做到这一点,即复制之类的东西
for i := 1 TO Length(s) - 1 do
a^[i] := Ord(s[i]);
delphi 7
德尔福 7
回答by Chau Chee Yang
Beware using the Move. If you are using Delphi 2009, it may fail. Instead, use this:
小心使用 Move。如果您使用的是 Delphi 2009,它可能会失败。相反,使用这个:
Move(s[1], a^, Length(s) * SizeOf(Char));
Move(s[1], a^, Length(s) * SizeOf(Char));
You may also use class TEncoding in SysUtils.pas (Delphi 2009/2010++ only) to perform the task.
您还可以使用 SysUtils.pas(仅限 Delphi 2009/2010++)中的类 TEncoding 来执行任务。
回答by Christopher Chase
never mind, found it
没关系,找到了
Move(s[1], a^, Length(s));
回答by Remko
You can simply cast it:
你可以简单地投射它:
a := @s[1];
The other way around is:
另一种方式是:
s := PChar(a);