用以下函数吧
转换后是一个数组,只要读取该数组的内容就行了
//将文件转换为一个byte数组
Procedure ConvFileToArray(FileName, Name: String);
Var
Size, I: Int64;
Buf: Char;
S: String;
Mem, Data: TMemoryStream;
Begin
Mem := TMemoryStream.Create;
Data := TMemoryStream.Create;
Data.LoadFromFile(FileName);
Data.Position := 0;
Size := Data.Size;
S := Format('Const' + #13#10 + ' %sSize = %d;' + #13#10 + ' %s:Array[0..%d] Of Byte = (',
[Name, Size, Name, Size - 1]);
Mem.WriteBuffer(S[1], Length(S));
I := 0;
While I < Size Do
Begin
Data.ReadBuffer(Buf, 1);
S := Format('$%s, ', [IntToHex(Ord(Buf), 2)]);
Mem.WriteBuffer(S[1], Length(S));
If ((I Mod 32) = 0) Then
Mem.WriteBuffer(#13#10#9, 3);
Inc(I);
End;
S := #13#10#9 + ');';
Mem.Position := Mem.Size - 2;
Mem.WriteBuffer(S[1], Length(S));
Mem.Position := 0;
Mem.SaveToFile(ExtractFilePath(ParamStr(0)) + ChangeFileExt(ExtractFileName(FileName), '.Inc'));
Data.Free;
Mem.Free;
End;
ConvFileToArray('C:/Windows/System32/MSComm32.OCX', 'MSComm32');