Ok, it has been a couple funny (not!) hours of trying this and that and finally managed to ascertain that it's
not the transposition that fails: that works well, one just have to save the result string to a file to see it.
What's failing is adding the string to the memo! Nothing works: Memo.Append(), Memo.Lines.Add(), Memo.Lines.AddText(), assigning Memo.Text, assigning Memo.Lines.Text, ... nothing!
And here is the even more curious thing: if the file constains only text, it works: the transposition simply replaces all line-endings with dots/spaces and the whole appears as a single line. But with a binary file, which in theory should do the same, nothing appears in the memo.
I'm completely surprised, atonit, flaberglasted.
Only explanation I can think of (and is as wild a guess as they come) is that the memo is trying to interpret the string as UTF-8 and failing miserably.
Next test: After converting the string, save it to a temp file and open
that file in the memo with
LoadFromFile(). But I'll leave that to you ... ok, no: here it is, in all its untested "glory":
const
ProblemChars = [#0..#31];
Placeholder = '.';
procedure ReadBinaryToMemo(const AFilename: String; const AMemo: TMemo);
var
AString: RawByteString;
i; Integer;
begin
AString := ReadFileToString(AFilename);
if AString = '' then
ShowMessage('Seems to be an empty file?')
else begin
{ Replace problematic chars by dots }
for i := 1 to Length(AString) do
if AString[i] in ProblemChars then
AString[i] := Placeholder;
{ Save the converted string to a temp file }
if FileExists('memotest.temp') then
DeleteFile('memotest.temp');
with TFileStream.Create('memotest.temp', fmCreate) do
try
{ Extremely simplistic, naïve approach }
if Write(AString[1], Length(AString)) <> Length(AString) then
ShowMessage('Didn''t write the full string :( ');
finally
Free;
end;
{and load that file into the memo}
try
AMemo.LoadFromFile('memotest.temp');
finally
DeleteFile('memotest.temp');
end;
end;
end;
(Maybe it has nothing to do with this thing, but I found earlier that the Memo component cannot load files that are in UTF-16 format, only with UTF-8. I don't know if it does matter in this case?)
No, it doesn't matter in this case but ... are you sure of that? I kind of remember having opened UTF16 files w/out problems. Of course my memory may be at fault here; I don' use many UTF16 files.