2013-06-10 6 views
0

델파이를 사용하여 .txt 파일을로드하고 이진 파일로 바이트 값을 읽으려고합니다. 나는 바이트로 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] 
+0

'x'가 바이트 인 경우, 'IntToHex (x, 2)'는 두 자리 16 진수의 텍스트 표현입니다. –

+0

감사합니다. 나는이 방법을 사용했고, 이제 내가 원했던 16 진수 값을 얻는다. 지금 바이너리로 변환하려고 노력할 것입니다. – HHH

+0

사용자가 OpenDialog에서 * Cancel * 버튼을 눌렀을 때 코드가 파일을로드하는 이유는 무엇입니까? –

답변

3

Byte는 숫자 (정수)를 입력합니다. ShowMessage에는 문자열이 필요합니다. 다른 정수 유형을 표시하는 것과 같은 방법으로 Byte을 문자열로 표시하십시오. IntToStr 또는 Format 또는 IntToHex 등 원하는 것을 모두 사용할 수 있습니다.

디버거에서 프로그램을 일시 중지하고 특수 코드를 작성하지 않고 변수의 값을 검사하여 프로그램에 표시되도록 할 수도 있습니다. 디버거는 변수의 유형과 명시 적 변환없이 표시하는 방법을 알고 있습니다.

+0

감사합니다. 나는 바이트 vars가 16 진수 값으로 저장된다고 가정하고 있었다. 당신의 대답은 그들이 INT 값으로 저장된다는 것을 깨닫게했습니다. 감사! – HHH

+1

@HHH HEX 나 INT로 저장되지 않았습니다. 그들은 바이트로 저장됩니다. 화면에서 바이트를보고 표시하는 방법은 저장소와 관계가 없습니다. –

3

어떻게 바이트의 배열로 파일의 내용을 보려면

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

를 호출에 대해?

uses 절에 ioutils를 추가했는지 확인하십시오.