[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1723
  • Last Modified:

Move a string to a TStringStream and vice-versa

What is the correct way to move the content of a string to a TStringStream and vice-versa
Also, I suppose that the string should not a declared unicode but ... ?
Thanks
0
LeTay
Asked:
LeTay
  • 2
1 Solution
 
Sinisa VukCommented:
you can use it both ways:
- at create:    
  strm := TStringStream.Create(sText_var);
- at later time:
  strm := TStringStream.Create('');
  strm.WriteString(sText_var);

read:
  strm.Position := 0;
  sResult := strm.ReadString(strm.Size);
0
 
LeTayAuthor Commented:
Many thanks for your quick answer, sinisav
It is "normal" that the string (sResult in your example) is unicode ?
Unicode means a even number of bytes so what happens if the stream contains an odd number of bytes ?
Thanks
0
 
Sinisa VukCommented:
As far I can see (in source :-) it is irrelevant what type of string is. Internally all goes with byte operation. If you put unicode string then you must read as unicode, same with ansistring. What version of Delphi you have? In newer Delphi (XEx) there is Encoding property which tells you what type of string is on, or when you before you write to stream set this prop. to right value.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now