Strings as “data buffers”

I bet most of the Delphi developers used or are still using strings to hold and operate on binary data. I’ve done it, and some of my older projects still contain such code. The basic idea is to use strings as byte buffers to hold and to use string routines to perform operations on those buffers. So what is the benefit of using strings as opposed to normal dynamic arrays? In my opinion (or better said in my use case) it was speed of development. I needed to prototype some code fast and did not care about the execution speed nor about the prettiness of the solution. Strings allow you to use all the normal functions such as Pos, Copy, Delete

If you are new to Delphi 2009 or 2010 and you use strings as “binary buffers” then you most probably had some additional work porting your application to the Unicode world.

You May Also Like

About the Author: Alexandru Ciobanu

5 Comments

  1. Using strings as buffers for binary data should have been abandoned when dynamic arrays were introduced. Oh, I know there is no Pos or concatenation for dynarrays, but if everyone had converted to them, and the demand had been there, someone (probably Borland) would have written those. Or one could have written them oneself.

    In other words: I have absolutely no remorse for those who got in trouble when strings became 16-bit based because they used strings as binary buffers. They had some 15 years (or more?) to do the right thing and didn’t.

  2. It took me a good 1 year to change our codebase in the past (when 2009 was released) to replace all string buffers for TBytes.

    Its a huge amount of work but its worth it.. I didn’t want to “ansify” my code by changing string to AnsiString to keep using it as data buffer

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.