Jump to content
William23668

How to set TBytes array to the file size ?

Recommended Posts

Hi

 

I am using TFileStream to read and write binary file and I want to read the entire file bytes and the address of each byte so I started like that:

 

procedure ReadBinary(filename: string);
var
  AFile: TFileStream;
  BR: TBinaryReader;
  AInteger: Integer;
  ADouble: Double;
  AChar: Char;
  AString: String;
  MyByteArray: TBytes;
  fsize: nativeint;
begin
  AFile := TFileStream.Create(filename, fmOpenRead);
  BR := TBinaryReader.Create(AFile, TEncoding.Unicode, false);
  try

     fsize := FileSize(AFile);
    // Declare our array to hold the returned data
    SetLength(MyByteArray, fsize);

    // Get the whole file contents into the byte array
    MyByteArray := BR.ReadBytes(FileSize(AFile));
    memo1.lines.add(TEncoding.Unicode.GetString(MyByteArray));


    // read an integer and write it to the console
    AInteger := BR.ReadInteger;
    memo1.lines.add('integer:'+AInteger.ToString);

    // read a double and write it to the console
    ADouble := BR.ReadDouble;
    memo1.lines.add('double:'+ADouble.ToString);

    // read a char and write it to the console
    AChar := BR.ReadChar;
    memo1.lines.add('char:'+AChar);
    // read a string and write it to the console
    AString := BR.ReadString;
    memo1.lines.add('string:'+AString);

    BR.Close;
  finally

    BR.Free;
    AFile.Free;
  end;
end;

 

I need to set the size of the MyByteArray to the size of the file so I used FileSize but it is NativeInt while I need ineteger how to convert NativeInt to Integer to set the length of the array ?

 

SetLength(MyByteArray, FileSize(AFile));

 

the code above for learning so it has more lines I dont need.

Edited by William23668

Share this post


Link to post
41 minutes ago, William23668 said:

// Declare our array to hold the returned data SetLength(MyByteArray, 33);

Why are you doing this? You are allocating 33 bytes and in the next line assign the variable to a new array that is returned by TBinaryReader.ReadBytes.

Share this post


Link to post
1 minute ago, Lajos Juhász said:

Why are you doing this? You are allocating 33 bytes and in the next line assign the variable to a new array that is returned by TBinaryReader.ReadBytes.

I edited the topic, I get error here:

 

SetLength(MyByteArray, fsize);

 

Share this post


Link to post

The size of the file is AFile.Size. You read the entire file into a byte array, but then go at the same stream with a binary reader. There are so many mistakes here. Do you want to read it into a byte array and covert to string. Or do you want to read it as binary. 

  • Thanks 1

Share this post


Link to post

If it's a textfile you can use memo.lines.loadfromfile or read it using TFileStream. For example:

 

var
  f: TFileStream;
  buff: TBytes;
begin
  f:=TfileStream.Create('d:\temp\test.txt', fmOpenRead+fmShareDenyNone);
  try
    SetLength(buff, f.Size);
    f.ReadBuffer(buff, f.Size);
    memo1.lines.text:=TEncoding.ANSI.GetString(buff);
  finally
    f.Free;
  end;
end;

 

  • Like 1

Share this post


Link to post
6 minutes ago, David Heffernan said:

The size of the file is AFile.Size. You read the entire file into a byte array, but then go at the same stream with a binary reader. There are so many mistakes here. Do you want to read it into a byte array and covert to string. Or do you want to read it as binary. 

read it as binary. I will want to store it into sqlite database later.

Share this post


Link to post
1 minute ago, William23668 said:

read it as binary. I will want to store it into sqlite database later.

In that case load it directly to the field you're going to use to store it in the database.

Share this post


Link to post
1 minute ago, Lajos Juhász said:

In that case load it directly to the field you're going to use to store it in the database.

by coding.

Share this post


Link to post

I tried now to use this code:

 

    SetLength(MyByteArray, AFile.Size);
    
    MyByteArray := BR.ReadBytes(AFile.Size);
    memo1.lines.add(TEncoding.Unicode.GetString(MyByteArray));

but I get something like this 婍 from exe file.

Edited by William23668

Share this post


Link to post
Just now, William23668 said:

but I get something like this 婍 

The file doesn't contains UTF-16LE text. You should check for the encoding of the file.

  • Like 1

Share this post


Link to post
7 minutes ago, William23668 said:

read it as binary. I will want to store it into sqlite database later.

Then why do you try to convert to a string as if it were UTF16? You seem very confused. 

 

2 minutes ago, William23668 said:

but I get something like this 婍 from exe file

Well, yeah, it probably isn't UTF16 is it? 

 

It looks like you tried 10 things at random and left them all in the code you posted. 

Edited by David Heffernan
  • Like 1

Share this post


Link to post

You already read every byte!! 

 

The real problem here is that you can't explain what you are trying to do. What do you mean about getting every address? An exe file isn't a list of addresses. 

Edited by David Heffernan

Share this post


Link to post
2 minutes ago, David Heffernan said:

You already read every byte!! 

 

The real problem here is that you can't explain what you are trying to do. What do you mean about getting every address? An exe file isn't a list of addresses. 

for example hex editor it display address and value ?

Share this post


Link to post

Well you have all of the information you need in a byte array. The index into the array is the address, and the byte value at that index is your value. Job done. 

  • Like 1

Share this post


Link to post

Here is the content (I assume by Gary Darby):

HexView will display the contents of any file using hexadecimal (base 16) digits.   There are 16 hexadecimal digits with values 0 through 15 but labeled "0" through "9" and "A" though "F" for convenience.  Each hex digit could also be written with 4 binary bits (0000 to 1111).   

 

 Hexadecimal numbers are convenient for use in displaying computer memory or files  because two of those 4 bit hex digits can represent the basic unit of memory storage, the 8-bit "byte"

 

I've had a version of this one floating around for my own use for years, but just recently realized that i had never posted it. 

 

Use of the program is self explanatory; , select a file to browse and use PgUp, PgDn keys to page through it.  Ctrl+PgUp will jump to page 1, Ctrl+PgDn will jump to the last page.  The "Esc"  escape key will close the file.  Characters which represent valid characters will be displayed on each line beside the hexadecimal data. 

 

Programmer's Notes

 

A TFileStream control is used to access the file to be displayed.   In order to avoid memory problems with large files, the amount of data (BufLen) to fill a page is calculated for each display operation based on the current size of the TMemo control.  For each page, procedure ShowPage seeks to the start of the next page to be displayed (CurPage*BufLen) and reads BufLen bytes of data.   Hexchars is the array containing the 16 hexadecimal character labels.  The Delphi code  uses a loop on N and J to convert each byte for each line into two display characters and add it to the string, Hex.  N reflects the position of the current line start within the buffer and J points tpo the current character being converted.  The conversion line of code looks like this:


hex:=hex+hexchars[(buffer[n+j] and $F0) shr 4] + hexchars[(buffer[n+j] and $0F)];

 

The left hand "nibble" (4 bits of a byte) is converted by "and"ing it with hex F0 (11110000 binary) to clear out the right side nibble and then shifted right (= divided by 16) to get it back in the range 0-15 which is then used as an index into the Hexchars array.  The right half of the bite is then converted similarly except that there's no need to divide by 16.HexViewSource.zip

 

The displayed lines are added to a TStringlist (List) whose strings are assigned  to the Memo1.Lines property after the page has been built.  This eliminated a flicker problem that occurred when lines were built directly into Memo1.

 

One more interesting problem that I ran into and which might save some time for you in the future.  When a control is aligned to the bottom of the form (like the TStatictext control in this case), and the form is resized to a smaller height, the control is placed below any existing controls.  Adjusting the sizes in the OnResize exit is too late to prevent this problem.   The solution is to use the OnCanResize exit to predict where the bottom aligned control will be and readjust the size and tops of the other controls in advance of the actual resize operation.. 

Edited by Lajos Juhász
  • Thanks 1

Share this post


Link to post
5 hours ago, William23668 said:

I want to read the entire file bytes

There is a function for that in System.IOUtils:

  MyByteArray := TFile.ReadAllBytes(filename);

 

Edited by Uwe Raabe
  • Like 2
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×