Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6964577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:56:32+00:00 2026-05-27T15:56:32+00:00

I am reading in a file, attempting to check if it is a binary

  • 0

I am reading in a file, attempting to check if it is a binary file by checking the first n bytes for a NUL byte, and if it is not determined to be binary that way, it is manipulated as a string. I tried to loop over a string and check the first n indices for a NUL, but that would give false positives that checking a TBytes does not.

I use TFile.ReadAllBytes, which returns a TBytes and perform the NUL check on that. Then if no NUL is found, I use StringOf on the TBytes to get a string. I was wondering if StringOf has to make a copy of the data to make a string out of it (these are large files so I want to avoid that) and if so, what is a better way to do what I am trying to do.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:56:33+00:00Added an answer on May 27, 2026 at 3:56 pm

    Does StringOf make a copy of the data passed to it?

    Yes, according to the docs: 'Converts a byte array into a Unicode string using the default system locale.'

    If you just want to access the TBytes as a string, why not cast it to a PChar (if it’s Unicode) or PAnsiChar if it’s an AnsiString?

    Example code:

    var
      MyBuffer: TBytes;
      BufferLength: integer;
      BufferAsString: PChar;
      BuferAsAnsiString: PAnsiChar;
    begin
      MyBuffer:= TFile.ReadAllBytes(Filename);
      BufferLength:= SizeOf(MyBuffer);
      BufferAsString:= PChar(@MyBuffer[0]);
      BufferAsAnsiString:= PAnsiChar(@MyBuffer[0]);
      //if there's no #0 at the end, make sure not to read past the end of the buffer!
    

    EDIT
    I’m a bit puzzled, why you’re not just using TFile.OpenRead to get a FileStream.
    Let’s assume you’ve got gigabyte(s) of data and you’re in a hurry.
    The Filestream will allow you to just read a small chunk of the data speeding things up.

    This example code reads the whole file, but can easily be modified to only get a small part:

    var
      MyData: TFileStream
      MyString: string;  {or AnsiString}
      FileSize: integer;
      Index: integer;
    begin
      MyData:= TFile.OpenRead(Filename);
      try
        FileSize:= MyData.GetSize;
        SetLength(MyString,FileSize+1); //Preallocate the string;
        Index:= 0;
        MyData.Read(PChar(MyString[Index])^, FileSize);
      finally
        MyData.Free;
      end;
      //Do stuff with your newly read string.  
    

    Note that the last example still reads all data from disk first (which may or may not be what your want).
    However you can also read the data in chunks.
    All of this is simpler with AnsiStrings because 1 char = 1 byte there :-).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i just create reading file feature. In general, what kind PHP class that usually
While implementing XML file reading/writing in my application I saw that when I call
I am reading a file byte-by-byte. Say for example i have this byte: 0x41
Are there any good configuration file reading libraries for C\C++ that can be used
I am having following issue with reading binary file in C. I have read
I developing a HTML5 Canvas App and it involves reading a xml file that
I'm reading in a binary file (a jpg in this case), and need to
So I'm attempting to create a Befunge interperter and reading a text file into
Currently I am attempting to open a text file that was saved in Unicode
Well, I am attempting to read a text file that looks like this: FTFFFTTFFTFT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.