William23668 8 Posted December 23, 2021 (edited) 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 December 23, 2021 by William23668 Share this post Link to post
Lajos Juhász 293 Posted December 23, 2021 Why do you think that you have to convert NativeInt to Integer? Share this post Link to post
William23668 8 Posted December 23, 2021 I was not clear sorry, in this line SetLength(MyByteArray, FileSize(AFile)); I get error because it expect Integer Share this post Link to post
Lajos Juhász 293 Posted December 23, 2021 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
William23668 8 Posted December 23, 2021 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
William23668 8 Posted December 23, 2021 (edited) I want to read the file bytes and address and display in a memo Edited December 23, 2021 by William23668 Share this post Link to post
David Heffernan 2345 Posted December 23, 2021 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. 1 Share this post Link to post
Lajos Juhász 293 Posted December 23, 2021 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; 1 Share this post Link to post
William23668 8 Posted December 23, 2021 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
Lajos Juhász 293 Posted December 23, 2021 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
William23668 8 Posted December 23, 2021 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
William23668 8 Posted December 23, 2021 (edited) 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 December 23, 2021 by William23668 Share this post Link to post
Lajos Juhász 293 Posted December 23, 2021 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. 1 Share this post Link to post
David Heffernan 2345 Posted December 23, 2021 (edited) 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 December 23, 2021 by David Heffernan 1 Share this post Link to post
William23668 8 Posted December 23, 2021 Long time I did not use Delphi. Any article to read about reading exe files ? I need to get each byte value and address. Share this post Link to post
David Heffernan 2345 Posted December 23, 2021 (edited) 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 December 23, 2021 by David Heffernan Share this post Link to post
William23668 8 Posted December 23, 2021 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
David Heffernan 2345 Posted December 23, 2021 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. 1 Share this post Link to post
Lajos Juhász 293 Posted December 23, 2021 You can take a look at http://delphiforfun.org/programs/utilities/hexview.htm. 1 Share this post Link to post
William23668 8 Posted December 23, 2021 3 minutes ago, Lajos Juhász said: You can take a look at http://delphiforfun.org/programs/utilities/hexview.htm. thanks alot. I can not open this url, does it work with you ? Share this post Link to post
Lajos Juhász 293 Posted December 23, 2021 (edited) 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 December 23, 2021 by Lajos Juhász 1 Share this post Link to post
Lajos Juhász 293 Posted December 23, 2021 It guess with the edit the source was removed. HexViewSource.zip 1 Share this post Link to post
William23668 8 Posted December 23, 2021 Just now, Lajos Juhász said: It guess with the edit the source was removed. HexViewSource.zip Appreciate your help friend. Share this post Link to post
Uwe Raabe 2057 Posted December 23, 2021 (edited) 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 December 23, 2021 by Uwe Raabe 2 1 Share this post Link to post