delphi 写内存问题 送上我所有的的分
现在有一个长度不定的字符串是hex我要定义个一个记录把这个记录送进内存后记录里面的内存格式为前四位为字符串长度后面的是和字符串一样的内存数据比如字符串0123456789...
现在有一个长度不定的字符串 是hex
我要定义个一个 记录 把这个 记录 送进内存后记录里面的内存格式为
前四位为字符串长度后面的是和字符串一样的内存数据
比如 字符串 0123456789ABCDEF
把记录送进内存后 内存的数据为 0F 00 01 23 45 67 89 AB CD EF
应该怎么写 大家给帮帮忙 我把所有的的分都送上了 展开
我要定义个一个 记录 把这个 记录 送进内存后记录里面的内存格式为
前四位为字符串长度后面的是和字符串一样的内存数据
比如 字符串 0123456789ABCDEF
把记录送进内存后 内存的数据为 0F 00 01 23 45 67 89 AB CD EF
应该怎么写 大家给帮帮忙 我把所有的的分都送上了 展开
1个回答
展开全部
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure SendToMem(S:String;AStream:TMemoryStream);
implementation
{$R *.dfm}
procedure SendToMem(S:String;AStream:TMemoryStream);
var
I,Buf: Integer;
L:Word;
A:Array of Byte;
begin
L:=Length(S);
AStream.WriteBuffer(L,sizeof(L));
SetLength(A,L div 2);
I:=1;
while I<=L do
begin
Buf:= StrToInt('$'+S[I]+S[I+1]);
AStream.WriteBuffer(Buf,1);
inc(I,2);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MS:TMemoryStream;
begin
//to test the function
MS :=TMemoryStream.Create;
try
SendToMem('123456789ABCDEF',MS);
MS.SaveToFile('d:\ttt.bin');
{to see if the result is right by writting the stream to a file
you can check the result by check the file with a hex editor such as ultraedit.}
finally
MS.Free;
end;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure SendToMem(S:String;AStream:TMemoryStream);
implementation
{$R *.dfm}
procedure SendToMem(S:String;AStream:TMemoryStream);
var
I,Buf: Integer;
L:Word;
A:Array of Byte;
begin
L:=Length(S);
AStream.WriteBuffer(L,sizeof(L));
SetLength(A,L div 2);
I:=1;
while I<=L do
begin
Buf:= StrToInt('$'+S[I]+S[I+1]);
AStream.WriteBuffer(Buf,1);
inc(I,2);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MS:TMemoryStream;
begin
//to test the function
MS :=TMemoryStream.Create;
try
SendToMem('123456789ABCDEF',MS);
MS.SaveToFile('d:\ttt.bin');
{to see if the result is right by writting the stream to a file
you can check the result by check the file with a hex editor such as ultraedit.}
finally
MS.Free;
end;
end;
end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询