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

File 在Delphi中将txt文件读入字节值,file,delphi,binary,byte,File,Delphi,Binary,Byte,我正在尝试使用Delphi加载一个.txt文件,并读取二进制文件的字节值。我正在尝试以字节形式获取txt数据,但我不确定它是否正常工作,因为我不知道如何显示字节值: begin // open dialog openDialog := TOpenDialog.Create(self); // Create the open dialog object - assign to our open dialog variable openDialog.InitialDir := GetCurr

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

Byte
是一种数字(整数)类型<代码>显示消息需要字符串。将
字节
显示为字符串,显示方式与显示任何其他整数类型相同。您可以使用
IntToStr
,或
Format
,或
IntToHex
,或任何您喜欢的方法

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

调用

var b:TBytes;
b := TFile.ReadAllBytes('file.txt');
将文件内容放入字节数组


只需确保将ioutils添加到uses子句。

如果
x
是一个字节,
IntToHex(x,2)
是它的两位十六进制文本表示形式。谢谢。我使用了这个方法,现在得到了我想要的十六进制值。我现在将尝试将其转换为二进制。当用户按下OpenDialog中的“取消”按钮时,为什么您的代码会加载文件?谢谢。我假设字节变量存储为十六进制值。你的回答让我意识到它们被存储为INT值。谢谢@它们既不是十六进制也不是整数。它们是以字节的形式存储的。如何选择在屏幕上查看和表示字节和存储无关。