关于delphi Move函数的用法详解
使用delphi多年,前些天忽然遇到不会string转pbyte,很是失落,此时对于编程基本功的重要性深有体会.这其中用到MOVE函数.
搞了好一会才搞明白其用法.所以想贴出来帮助需要帮助的人.
var
s:string;
ps:Pchar;
b:pbyte;
len:integer;
begin
s:=edit1.Text; //字符串
ps:=pchar(s); //转成pchar类型,
len:=length(s);//取字符串长度,占用多少字节
getmem(b,len);//申请内存,pchar,pbyte在使用前都必须要申请内存,因为他们是指针.
move(ps^,b^,len);//这里 ps^意思是pchar指向内存数据的第一个字节地址,B^是表示申请内存的第一个字节地址,这样就可以一个一个字节的移到b里去了.
memo1.Text:=pchar(b);//显示.
freemem(b);
end;
有些人遇到的困惑是为什么 move(s,b,len)不行呢?同样我也遇到这样的困惑.
看了一样move的函数源码才明白.
procedure Move( const Source; var Dest; count : Integer );
{$IFDEF PUREPASCAL}
var
S, D: PChar;
I: Integer;
begin
S := PChar(@Source);//取内存地址
D := PChar(@Dest);//取内存地址
if S = D then Exit;
if Cardinal(D) > Cardinal(S) then
for I := count-1 downto 0 do
D[I] := S[I]
else
for I := 0 to count-1 do
D[I] := S[I];
end;
如果直接传入s,
S := PChar(@Source);//取内存地址\
就相当于取的字符串S地址的地址.
如果传入的是ps^
S := PChar(@Source);//取内存地址
就相当于取pchar 所指向字符串实际数据的地址.
相关文档:
type
TCharStack = class(TStack)
private
function GetTop: Char;
public
function Pop: Char;
function Push(Item: Char): Char;
property Top: Char read GetTop;
end;
const
FindSet = ['(',')'];
implementation
{$R *.dfm}
{ TCharStack }
......
delphi 注册 com 对象的方法
procedure TForm1.Button3Click(Sender: TObject);
var
Sd: TSecurityDescriptor;
begin
InitializeSecurityDescriptor(@Sd, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@Sd, true, Nil, false);
RegSe ......
http://meidi152.blog.163.com/blog/static/5423302009610103610744/
1. memo控件读取txt
memo1.Lines.LoadfromFile('E:\*\*.txt');
2.
Procedure NewTxt(FileName:String);
Var
F : Textfile;
Begin
if fileExists(FileName) then DeleteFile(FileName); {看文件是否存在,在就刪除}
AssignFile(F, ......
Delphi in a Unicode World Part II: New RTL Features and
Classes to Support Unicode
By: Nick
Hodges
原文链接:http://dn.codegear.com/article/38498
Abstract: This article will cover the new features of the Tiburon
Runtime Library that will help handle Unicode strings.
//
& ......
var
arrChar : array [0..4] of Char;
b : Byte;
s : string;
begin
s := 'Test';
Move(Pointer(s)^, arrChar, Length(s)); //string to array of char
ShowMessage(arrChar);
b := Ord(s[1]); //First byte string to one single byte
......