I'm trying to make a basic Hex viewer from TMemo, I know that this is probably not perfect, but I will use it only personally so that it does not matter.
(1)
First, suppose Memo is filled with hexadecimal information like this:

How can I get the number of all displayed text blocks ignoring a space? Thus, using the image, the result in this case will be 28.
This is what I tried, and I know that this is completely wrong, as I access Memo strings, but I don’t know how to access each character.
I can not solve this simple problem :(
function CountWordBlocks(Memo: TMemo): Integer; var i: Integer; vCount: Integer; begin for i := 0 to Memo.Lines.Count - 1 do begin if Length(Memo.Lines.Strings[i]) = 2 then begin Inc(vCount); end; end; Result := vCount; end;
Here is the code that I use to display Hex values in Memo:
procedure ReadFileAsHex(const AFileName: string; ADestination: TStrings); var fs: TFileStream; buff: Byte; linecount: Byte; line: string; begin linecount := 0; line := ''; fs := TFileStream.Create(AFileName, fmOpenRead); try ADestination.BeginUpdate; try while fs.Position < fs.Size do begin fs.Read(buff, 1); line := line + IntToHex(buff, 2) + ' '; Inc(linecount); if linecount = 16 then begin ADestination.Add(line); line := ''; linecount := 0; end; end; if Length(line) <> 0 then ADestination.Add(line); finally ADestination.EndUpdate; end; finally fs.Free; end; end;
(2)
If I click Memo and the text block is under the cursor, how can I find out which number of the highlighted block belongs to everyone else?
Thus, using the same first image, the carriage is on the top line next to 68, so the result will be 3, as this is the third text block of 28.
It should be so simple, but I can’t think clearly, I don’t have the right programming yet, and I am really struggling with basic logic and problem solving!
(3)
Finally, I would like to select a block at runtime by passing the value of the block number. I tried this without much success:
procedure FindBlock(Memo: TMemo; BlockNumber: Integer); var i: Integer; txt: string; ThisWhite, PrevWhite: boolean; vRead: Integer; begin txt := Memo.Text; vRead:= 0; PrevWhite := True; for i := 1 to Length(txt) do begin ThisWhite := Character.IsWhiteSpace(txt[i]); if PrevWhite and not ThisWhite then begin Inc(vRead); PrevWhite := False; end; PrevWhite := ThisWhite; if vRead = BlockNumber then begin Memo.SelStart := vRead; Memo.SetFocus; Exit; end; end; end;