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

  • Home
  • SEARCH
  • 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 3236068
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:34:45+00:00 2026-05-17T17:34:45+00:00

Delphi 2009 and above uses unicode strings for their default string type. To my

  • 0

Delphi 2009 and above uses unicode strings for their default string type. To my understanding unicode char is actually 16 bit value or 2 bytes (note: I understand there is possibility of 3 or 4 bytes char, but let’s consider the most usual case). However I found that TStringStream is not very reliable to manipulating this strings. For example, TStringStream.Size property returns the length of the string, while I think it should return the byte count of the contained string. Okay, you can adjust it on your own, but the thing that really confused me the most is: TStringStream does not read from or write to a buffer reliably.

Please check the following code (it’s a DUnit test and always fail). Please let me know where the problem is (I was using D2010 when testing the code).

procedure TestTCPackage.TestStringStream;
const
  cCount = 10;
  cOrdMaxChar = Ord(High(Char));
var
  B: Pointer;
  SW, SR: TStringStream;
  T: string;
  i, j, k : Integer;
  vStrings: array [0..cCount-1] of string;
begin
  RandSeed := GetTickCount;
  for i := 0 to cCount - 1 do
  begin
    j := Random(100) + 1;
    SetLength(vStrings[i], j);
    for k := 1 to j do
      // fill string with random char (but no #0)
      vStrings[i][k] := Char(Random(cOrdMaxChar-1) + 1);
  end;

  for i := 0 to cCount - 1 do
  begin
    SW := TStringStream.Create(vStrings[i]);
    try
      GetMem(B, SW.Size * SizeOf(Char));
      try
        SW.Read(B^, SW.Size * SizeOf(Char));

        SR := TStringStream.Create;
        try
          SR.Write(B^, SW.Size * SizeOf(Char));
          SR.Position := 0;

          // check the string in the TStringStream with original value
          Check(SR.DataString = vStrings[i]);
        finally
          SR.Free;
        end;
      finally
        FreeMem(B);
      end;
    finally
      SW.Free;
    end;
  end;
end;

Note: I already tried to use an instance of TMemoryStream as intermediary from reading/writing the buffer and use CopyFrom of the TStringStream to read the content of that TMemoryStream with same failing effect.

  • 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-17T17:34:45+00:00Added an answer on May 17, 2026 at 5:34 pm

    After reading this post (and thanks to Serg who provided the answer to that question) and Barry Kelly’s answer, I have found the problem. TStringStream is actually using ASCII/ansistring encoding by default. So even if your default string type is unicode, unless you spesifically tell it to, it won’t use unicode encoding. Personally I think it’s strange. Maybe for making it easier to convert old codes.

    So you have to specifically set the encoding of the TStringStream to TEncoding.Unicode to manipulate unicode string properly.

    Here is my modified code which passes DUnit test is:

    procedure TestTCPackage.TestStringStream;
    const
      cCount = 10;
      cOrdMaxChar = Ord(High(Char));
    var
      B: Pointer;
      SW, SR: TStringStream;
      i, j, k : Integer;
      vStrings: array [0..cCount-1] of string;
    begin
      RandSeed := GetTickCount;
      for i := 0 to cCount - 1 do
      begin
        j := Random(100) + 1;
        SetLength(vStrings[i], j);
        for k := 1 to j do
          // fill string with random char (but no #0)
          vStrings[i][k] := Char(Random(cOrdMaxChar-1) + 1);
      end;
    
      for i := 0 to cCount - 1 do
      begin
        SW := TStringStream.Create(vStrings[i], ***TEncoding.Unicode***);
        try
          GetMem(B, SW.Size);
          try
            SW.ReadBuffer(B^, SW.Size);
    
            SR := TStringStream.Create('', ***TEncoding.Unicode***);
            try
              SR.WriteBuffer(B^, SW.Size);
              SR.Position := 0;
    
              // check the string in the TStringStream with original value
              Check(SR.DataString = vStrings[i]);
            finally
              SR.Free;
            end;
          finally
            FreeMem(B);
          end;
        finally
          SW.Free;
        end;
      end;
    end;
    

    Last note: Unicode does bite! 😀

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

Sidebar

Related Questions

No related questions found

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.