一个关于WideString和PByteArray的问题,请教大家.

seamansoft 2007-09-02 11:20:52
下面是我的一段代码.
问题是这样的: 我想实现以下过程 string -> WideString -> ByteArray -> WideString -> string.
但是中间出了问题,输入的string和得到的string并不相同.
以前我是学C++的,后来改用Delphi了,但Pascal基础不好.请大家多多指教.
procedure TfmMain.btnTestClick(Sender: TObject);
var
W, R: WideString;
S: string;
P: PByteArray;
I: Integer;
begin
S := '中国人123';
I := Length(S);
P := AllocMem(I * 2);
try
W := S;
Move(W[1], P^[3], I);
SetLength(R, I);
Move(P^[3], R[1], I);
S := R;
ShowMessage(Format('[%s]', [S]);
finally
FreeMem(P);
end;
end;
...全文
256 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
seamansoft 2007-09-02
  • 打赏
  • 举报
回复
不好意思,打错个地方,又不知道如何修改,所以写在下面.
procedure TfmMain.btnTestClick(Sender: TObject);
var
W, R: WideString;
S: string;
P: PByteArray;
I: Integer;
begin
S := '中国人123';
I := Length(S);
P := AllocMem(I * 2);
try
W := S;
Move(W[1], P^[0], I);
SetLength(R, I);
Move(P^[0], R[1], I);
S := R;
ShowMessage(Format('[%s]', [S]);
finally
FreeMem(P);
end;
end;
seamansoft 2007-09-02
  • 打赏
  • 举报
回复
请问一下如何结贴呀.
seamansoft 2007-09-02
  • 打赏
  • 举报
回复
确实是这样的.
sdfsoft 2007-09-02
  • 打赏
  • 举报
回复
应该是widestring和string的长度的问题.
seamansoft 2007-09-02
  • 打赏
  • 举报
回复
问题已解决.
procedure TfmMain.btnTestClick(Sender: TObject);
var
W, R: WideString;
S: string;
P: PByteArray;
I: Integer;
begin
S := '中国人123 ';
W := S;
I := Length(W);
P := AllocMem(I * 2);
try
Move(W[1], P^[0], I * 2);
SetLength(R, I);
Move(P^[0], R[1], I * 2);
S := R;
ShowMessage(Format( '[%s] ', [S]));
finally
FreeMem(P);
end;
end;

16,749

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧