在 Delphi 中将 txt 文件读入字节值

HHH*_*HHH 2 delphi binary byte file

我正在尝试使用 Delphi 加载 .txt 文件并以二进制形式读取文件的字节值。我正在尝试以字节形式获取 txt 数据,但我不确定它是否有效,因为我不知道如何显示字节值:

    begin
// open dialog
openDialog := TOpenDialog.Create(self); // Create the open dialog object - assign to our open dialog variable
openDialog.InitialDir := GetCurrentDir;    // Set up the starting directory to be the current one
openDialog.Options := [ofFileMustExist];     // Only allow existing files to be selected
if openDialog.Execute   // Display the open file dialog
then ShowMessage('The file you chose is : '+openDialog.FileName)
else ShowMessage('Open file was cancelled');


// assign file.
fileName:= openDialog.FileName;
AssignFile(myFile, fileName);  //ink a file on a disk to a file variable in our program
Reset(myFile);  //open an existing file or Rewrite to create a new file

// get file length.
fileLength:= FileSize(myFile);


while not Eof (myFile) do
begin

begin
Read(myFile,x);       // read file byte by byte

ShowMessage(x);       //display the data. I'm getting an error here because ShowMessage takes a string value. Tried converting it but I can't find out how to display the binary value of the byte x
.... // [ manipulation code of binary values of bytes goes here. Not sure if this works because I don't know what x is at the moment]
Run Code Online (Sandbox Code Playgroud)

Wou*_*ick 5

打电话怎么样

var b:TBytes;
b := TFile.ReadAllBytes('file.txt');
Run Code Online (Sandbox Code Playgroud)

要将文件的内容转换为字节数组?

只需确保将 ioutils 添加到您的使用子句中。