如果这样,就更简单了,强制转换就行,看来是我把问题想复杂了。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
const BUF_SIZE=1024;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
f:file of byte;
implementation
{$R *.DFM}
function Str2Hex(const data: array of byte; len: integer): string;
const
Hex:array[0..15] of char='0123456789ABCDEF';
var
i: Integer;
begin
setlength(result,length(data)*2);
for i := 0 to Len-1 do
begin
result[i*2+1]:=Hex[data shr 4];
result[i*2+2]:=Hex[data and $F];
end;
end;
procedure GetData(const data: array of byte); //注意data从data[0]开始
var
w: word;
dw: dword;
b2: array[0..1]of byte;
b4: array[0..3]of byte;
i, j: integer;
begin
for i := 1 to 3 do
begin
b2[0] := data[i*2-2];
b2[1] := data[i*2-1];
w := word(b2);
Form1.Memo2.Lines.Add(format('b[%d]: %2x,b[%d]: %2x, word: %d, 原值:%g',[i*2-2,b2[0],i*2-1,b2[1],w,w/100]));
end;
for j := 1 to 3 do
for i := (j*3-2) to (j*3+1) do
begin
b4[0] := data[i*4+2];
b4[1] := data[i*4+3];
b4[2] := data[i*4+4];
b4[3] := data[i*4+5];
dw:=DWORD(b4);
Form1.Memo2.Lines.Add(format('b[%2d]: %2x,b[%2d]: %2x,b[%2d]: %2x,b[%2d]: %2x,dword: %u, 原值:%g',
[i*4+2,b4[0],i*4+3,b4[1],i*4+4,b4[2],i*4+5,b4[3],dw,dw/1000]));
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
NumRead:integer;
b:array[1..BUF_SIZE] of byte;
s: string;
begin
assignfile(f,'T0105.101');
Reset(f);
repeat
BlockRead(f,b,BUF_SIZE,NumRead);
if NumRead=0 then break;
GetData(b);
s:=Str2Hex(b,NumRead); //将每个字节以16进制表示
Memo1.Lines.Add(s);
until (NumRead = 0);
closefile(f);
Memo1.Lines.SaveToFile('a.txt');
end;
end.