When downloading a pdf file in TFileStream encoding with hieroglyphs, how to fix it? code:

sStream := TFileStream.Create(aPDF, fmOpenRead or fmShareDenyWrite); GetMem(pBuf, sStream.size); cnt := sStream.Read(pBuf^, sstream.size); 

as a result:

 pBuf '倥䙄ㄭ㈮ഠㄊ〠漠橢'#$0A0D'㰼'#$0A0D'启灹'#$2065'䌯瑡污杯'#$0A0D'.... и т.д. 

due to incorrect encoding further check does not work

skin code

    2 answers 2

    The PDF header is not a Unicode string, so interpreting the data in such a way that pBuf, a pointer to wide characters, leads to misinterpretation. Here with the use of AnsiChar everything becomes normal:

    enter image description here

      Yes, your encoding is wrong, but not in the place where you think.

      The buffer read from the file is OK, and the problem is with the constant %PDF , which is interpreted as a Unicode string (if you use StrlComp from SysUtils ).

      To correct the error, you need to declare a constant as AnsiString and use a pointer to it, and use CompareMem instead of StrlComp , since you are comparing memory.

       const CPdfMagic: AnsiString = "%PDF"; var pBuf: Pointer; // или PAnsiChar begin ... if CompareMem(pBuf, @CPdfMagic[1], 4) then begin ...