2

我正在尝试使用 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]
4

2 回答 2

5

怎么打电话

var b:TBytes;
b := TFile.ReadAllBytes('file.txt');

将文件的内容放入字节数组中?

只需确保将 ioutils 添加到您的 uses 子句中即可。

于 2013-06-10T23:40:35.150 回答
4

Byte是数字(整数)类型。ShowMessage需要一个字符串。Byte以与显示任何其他整数类型相同的方式将 a 显示为字符串。您可以使用IntToStr, 或Format, or IntToHex, 或您喜欢的任何其他内容。

您也可以在调试器中暂停您的程序并检查变量的值,而不是编写特殊代码来让您的程序显示它。调试器知道变量的类型以及如何在没有显式转换的情况下显示它们。

于 2013-06-10T22:11:34.767 回答