Delphi - 读取文本文件中的十六进制值并将其放入 LongWord 中
Delphi - read a Hex value in a text file and put it in a LongWord
我的项目需要一点帮助。我有一个名为 Script.txt 的文本文件,里面有:
0000FCA2:FA2C
0000BC8D:F21C
我需要将“$0000FCA2”放入名为 "Address" 的 LongWord 中。并将 '$FA2C' 放入名为 "HexValue" 的 LongWord 中。而且,在第二行我必须做同样的事情。 '$0000BC8D' 在另一个名为 "Address2" 的 LongWord 中,与第一个一样,我必须将 '$F21C' 放入 "HexValue2" LongWord 中。我该怎么做?
抱歉,我上周才开始 Delphi,所以我有很多疑问。我用谷歌搜索了我的问题,但没有找到好的答案。对不起,错误的句子,英语不是我的母语。
非常感谢!
您可以创建自己的 lass 读取和解析文件:
uses
System.Generics.Collections;
Type
TDataLine = class
private
FHexValue: LongWord;
FAddress2: LongWord;
public
constructor Create(const aLine: String);
property HexValue: LongWord read FHexValue;
property Address2: LongWord read FAddress2;
end;
TDataFile = class(TObjectList<TDataLine>)
public
constructor Create; reintroduce;
procedure LoadFromFile(const aFlieName: String);
end;
{ TDataLine }
constructor TDataLine.Create(const aLine: String);
var
Buffer: TArray<string>;
Tmp: Int64;
begin
Buffer := aLine.Split([':'], TStringSplitOptions.ExcludeEmpty);
if TryStrToInt64('$' + Buffer[0], Tmp) then
FHexValue := Tmp;
if TryStrToInt64('$' + Buffer[1], Tmp) then
FAddress2 := Tmp;
end;
{ TDataFile }
constructor TDataFile.Create;
begin
inherited Create(True);
end;
procedure TDataFile.LoadFromFile(const aFlieName: String);
var
DataLine: TDataLine;
Buffer: TStringlist;
sLine: String;
begin
Buffer := TStringlist.Create;
try
Buffer.LoadFromFile(aFlieName);
for sLine in Buffer do
Add(TDataLine.Create(sLine));
finally
FreeAndNil(Buffer);
end;
end;
然后这样称呼它:
procedure TForm19.FormCreate(Sender: TObject);
var
DataFile: TDataFile;
begin
DataFile := TDataFile.Create;
DataFile.LoadFromFile('Data.txt');
Caption := DataFile.Count.ToString;
DataFile.Free;
end;
一个用于教育目的的简单示例:
type
TAddress = record
Address : LongWord;
Value : LongWord;
end;
TVarAddress = array of TAddress;
var Address : TVarAddress;
procedure GetAdressesFromTextFileIntoDynArray(FileName : String; var Arr : TVarAddress);
var
F : TextFile;
s, sAddr,sVal: string;
len,p : integer;
begin
Assign(F,FileName);
try
SetLength(Arr,0); //clear the array
Reset(F); //open text file
len := 0;
while not Eof(F) do
begin
Readln(F,s); //read line from text file
p := Pos(':',s); // get position of ":"
sAddr := Copy(s,1,p - 1); //get address from string
sVal := Copy(s,p+1,Length(s) - p); //get value from string
inc(len);
SetLength(Arr,len); //increment length of array
Arr[len -1].Address := StrToInt('$' + sAddr);
Arr[len -1].Value := StrToInt('$' + sVal);
end;
finally
CloseFile(F);
end;
end;
使用程序举例:
GetAdressesFromTextFileIntoDynArray('c:\TextFile.txt', Address);
for I := 0 to Length(Address) - 1 do
begin
Memo1.Lines.Add(IntToStr(Address[i].Address));
Memo1.Lines.Add(IntToStr(Address[i].Value));
end;
我的项目需要一点帮助。我有一个名为 Script.txt 的文本文件,里面有:
0000FCA2:FA2C 0000BC8D:F21C
我需要将“$0000FCA2”放入名为 "Address" 的 LongWord 中。并将 '$FA2C' 放入名为 "HexValue" 的 LongWord 中。而且,在第二行我必须做同样的事情。 '$0000BC8D' 在另一个名为 "Address2" 的 LongWord 中,与第一个一样,我必须将 '$F21C' 放入 "HexValue2" LongWord 中。我该怎么做?
抱歉,我上周才开始 Delphi,所以我有很多疑问。我用谷歌搜索了我的问题,但没有找到好的答案。对不起,错误的句子,英语不是我的母语。
非常感谢!
您可以创建自己的 lass 读取和解析文件:
uses
System.Generics.Collections;
Type
TDataLine = class
private
FHexValue: LongWord;
FAddress2: LongWord;
public
constructor Create(const aLine: String);
property HexValue: LongWord read FHexValue;
property Address2: LongWord read FAddress2;
end;
TDataFile = class(TObjectList<TDataLine>)
public
constructor Create; reintroduce;
procedure LoadFromFile(const aFlieName: String);
end;
{ TDataLine }
constructor TDataLine.Create(const aLine: String);
var
Buffer: TArray<string>;
Tmp: Int64;
begin
Buffer := aLine.Split([':'], TStringSplitOptions.ExcludeEmpty);
if TryStrToInt64('$' + Buffer[0], Tmp) then
FHexValue := Tmp;
if TryStrToInt64('$' + Buffer[1], Tmp) then
FAddress2 := Tmp;
end;
{ TDataFile }
constructor TDataFile.Create;
begin
inherited Create(True);
end;
procedure TDataFile.LoadFromFile(const aFlieName: String);
var
DataLine: TDataLine;
Buffer: TStringlist;
sLine: String;
begin
Buffer := TStringlist.Create;
try
Buffer.LoadFromFile(aFlieName);
for sLine in Buffer do
Add(TDataLine.Create(sLine));
finally
FreeAndNil(Buffer);
end;
end;
然后这样称呼它:
procedure TForm19.FormCreate(Sender: TObject);
var
DataFile: TDataFile;
begin
DataFile := TDataFile.Create;
DataFile.LoadFromFile('Data.txt');
Caption := DataFile.Count.ToString;
DataFile.Free;
end;
一个用于教育目的的简单示例:
type
TAddress = record
Address : LongWord;
Value : LongWord;
end;
TVarAddress = array of TAddress;
var Address : TVarAddress;
procedure GetAdressesFromTextFileIntoDynArray(FileName : String; var Arr : TVarAddress);
var
F : TextFile;
s, sAddr,sVal: string;
len,p : integer;
begin
Assign(F,FileName);
try
SetLength(Arr,0); //clear the array
Reset(F); //open text file
len := 0;
while not Eof(F) do
begin
Readln(F,s); //read line from text file
p := Pos(':',s); // get position of ":"
sAddr := Copy(s,1,p - 1); //get address from string
sVal := Copy(s,p+1,Length(s) - p); //get value from string
inc(len);
SetLength(Arr,len); //increment length of array
Arr[len -1].Address := StrToInt('$' + sAddr);
Arr[len -1].Value := StrToInt('$' + sVal);
end;
finally
CloseFile(F);
end;
end;
使用程序举例:
GetAdressesFromTextFileIntoDynArray('c:\TextFile.txt', Address);
for I := 0 to Length(Address) - 1 do
begin
Memo1.Lines.Add(IntToStr(Address[i].Address));
Memo1.Lines.Add(IntToStr(Address[i].Value));
end;