procedure TForm1.Button1Click(Sender: TObject);
var
 i,i2:Integer ;
 data:array of byte;
 s:string;
 hfile:THandle;
 byteswrite:dWord;
begin
 s:='486F6D65B9E6B8F1A3BA';
 i2:=Length(s) div 2;
 SetLength(data,i2);
 for i:=0 to Length(data)-1  do
 begin
 data[i]:=StrToInt('$'+copy(s,i*2+1,2));
 Edit1.Text :=Edit1.Text + inttohex(data[i],2);
 end;
   hFile := CreateFile('d:\xxx.bmp',GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ,nil,CREATE_ALWAYS,0,0);
   if hFile = INVALID_HANDLE_VALUE then Exit;
   WriteFile(hFile,data,Length(data)+1,BytesWrite,nil);
   CloseHandle(hFile);
end;
-----------------------
为什么我这段代码写进去的内容跟我预期不一样
486F6D65B9E6B8F1A3BA
变成
6426D5006CF71200D6F9但是如果将数组写成: data:array[0..9] of byte = ($48,$6F ...)
这样可以写入的,而且是正确的

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
    i,i2:Integer ;
    data:array of byte;
    s:string;
    hfile:THandle;
    byteswrite:dWord;
    FS : TFileStream;
    begin
    s:='486F6D65B9E6B8F1A3BA';
    i2:=Length(s) div 2;
    SetLength(data,i2);
    for i:=0 to Length(data)-1 do
    begin
    data[i]:=StrToInt('$'+copy(s,i*2+1,2));
    Edit1.Text :=Edit1.Text + inttohex(data[i],2);
    end;
    fs:=TFileStream.Create('d:\xxx.bmp',fmCreate or fmShareDenyRead);
    fs.Write(pchar(@data[0])^,Length(data));
    freeAndNil(fs);
    end;