CnPack Forum


 
Subject: 数组与字符串内容的互相转换规则
kendling (小冬)
高级版主
Rank: 8Rank: 8
MyvNet


Medal No.1  
UID 703
Digest Posts 5
Credits 978
Posts 580
点点分 978
Reading Access 101
Registered 2005-2-18
Location 广东
Status Offline
Post at 2007-11-21 16:21  Profile | Site | Blog | P.M.  | QQ | Yahoo!
数组与字符串内容的互相转换规则

作者:小冬 (kend)
日期:2007-11-21
原文出处:http://myvnet.com/article.asp?id=13
http://myvnet.com/
http://www.cnpack.org/

数组与字符串内容的互相转换往往采用Move或CopyMemory来进行,我总结了一下它们的规则: 1.Move时如果访问的是string或string[n],要用string[1],因为string[0]是字符串的长度,所以所有string都以string[1]开始。
2.Move时如果访问的是array,静态array(array[0..n] of Char)时直接使用,动态array(array of Char)时使用array[0]。因为动态array时变量名其实只是一个指针地址,不代表数组内容的地址。
3.Move时如果访问的是PChar,要使用^取指向的内容。 Move时如果目标是数组的话:
1.静态数组array[0..n] of Char,取两者最小长度直接Move。
2.动态数组array of Char,取string长度并把数组SetLength后再Move 如果目标是string,无论是string或string[n]都要先SetLength。 如果目标是PChar,要用GetMemory申请内存,使用完后可别忘了FreeMemory。 (以上string指AnsiString,string[n]指ShortString) 下面是简单的一点测试代码,我没有添加结果输出,大家可以自己用F8跟踪看结果:

// 字符串到数组的转换
procedure StringToArray;
var
  s1: string;
  s2: string[20];
  buf1: array[0..19] of Char;
  buf2: array of Char;
  len1, len2: Integer;
  procedure cls;
  begin
    s1 := '';
    s2 := '';
    buf1 := '';
    SetLength(buf2, 0);
    SetLength(buf2, 20);
    len1 := 0;
    len2 := 0;
  end;
begin
  // string to char array
  cls;
  s1 := 'http://MyvNet.com';
  len1 := Length(s1);
  len2 := Length(buf1);
  Move(s1[1], buf1, Min(len1, len2));
  // string to dynamic char array
  cls;
  s1 := 'http://MyvNet.com';
  len1 := Length(s1);
  len2 := Length(buf2);
  Move(s1[1], buf2[0], Min(len1, len2));
  // string array to char array
  cls;
  s2 := 'http://MyvNet.com';
  len1 := Length(s2);
  len2 := Length(buf1);
  Move(s2[1], buf1, Min(len1, len2));
  // string array to dynamic char array
  cls;
  s2 := 'http://MyvNet.com';
  len1 := Length(s2);
  len2 := Length(buf2);
  Move(s2[1], buf2[0], Min(len1, len2));
  cls;
end;
// 数组到字符串的转换
procedure ArrayToString;
var
  s1: string;
  s2: string[20];
  buf1: array[0..19] of Char;
  len1, len2: Integer;
  procedure cls;
  begin
    s1 := '';
    s2 := '';
    buf1 := '';
    SetLength(buf2, 0);
    SetLength(buf2, 20);
    len1 := 0;
  end;
begin
  // char array to string
  cls;
  buf1 := 'http://MyvNet.com';
  len1 := Length(buf1);
  SetLength(s1, len1);
  Move(buf1, s1[1], len1);
  // char array to string array
  cls;
  buf1 := 'http://MyvNet.com';
  len1 := Length(buf1);
  SetLength(s2, len1);
  Move(buf1, s2[1], len1);
  // or
  //Move(buf1, s2[1], len1);
  //s2[0] := Chr(len1);
  // dynamic char array to string
  cls;
  buf1 := 'http://MyvNet.com';
  Move(buf1, buf2[0], Length(buf1));
  len1 := Length(buf2);
  SetLength(s1, len1);
  Move(buf2[0], s1[1], len1);
  // dynamic char array to string array
  cls;
  buf1 := 'http://MyvNet.com';
  Move(buf1, buf2[0], Length(buf1));
  len1 := Length(buf2);
  SetLength(s2, len1);
  Move(buf2[0], s2[1], len1);
  // or
  //Move(buf2[0], s2[1], len1);
  //s2[0] := Chr(len1);
  cls;
end;
// PChar和数组的转换
procedure TForm1.PCharAndArray;
var
  pc: PChar;
  buf1: array[0..19] of Char;
  buf2: array of Char;
  len1: Integer;
  procedure cls;
  begin
    pc := '';
    buf1 := '';
    SetLength(buf2, 0);
    SetLength(buf2, 20);
    len1 := 0;
  end;
begin
  // PChar to array
  cls;
  pc := 'http://MyvNet.com';
  len1 := Length(pc);
  Move(pc^, buf1, len1);
  // PChar to dynamic array
  cls;
  pc := 'http://MyvNet.com';
  len1 := Length(pc);
  Move(pc^, buf2[0], len1);
  // array to PChar
  cls;
  buf1 := 'http://MyvNet.com';
  len1 := Length(buf1);
  pc := GetMemory(len1);
  Move(buf1, pc^, len1);
  FreeMemory(pc);
  // dynamic array to PChar
  cls;
  buf1 := 'http://MyvNet.com';
  Move(buf1, buf2[0], Length(buf1));
  len1 := Length(buf2);
  pc := GetMemory(len1);
  Move(buf2[0], pc^, len1);
  FreeMemory(pc);
  cls;
end;





小冬
http://MyvNet.com
Top
smonkey421 (猩少)
新警察
Rank: 1



UID 27509
Digest Posts 0
Credits 15
Posts 6
点点分 15
Reading Access 10
Registered 2007-9-28
Status Offline
Post at 2007-11-21 16:43  Profile | Blog | P.M. 
收藏~
Top
skyjacker
版主
Rank: 7Rank: 7Rank: 7
茶农


UID 2239
Digest Posts 9
Credits 617
Posts 269
点点分 617
Reading Access 100
Registered 2006-6-8
Status Offline
Post at 2007-11-21 16:52  Profile | Blog | P.M.  | QQ


1.MOVE 操作的是“块”, move 内部会取地址
2.MOVE 的目标必须提前分配空间

使用时确实要注意的




一壶清茶煮青春.
Top
 




All times are GMT++8, the time now is 2024-11-22 11:26

    本论坛支付平台由支付宝提供
携手打造安全诚信的交易社区 Powered by Discuz! 5.0.0  © 2001-2006 Comsenz Inc.
Processed in 0.014336 second(s), 9 queries , Gzip enabled

Clear Cookies - Contact Us - CnPack Website - Archiver - WAP