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
LeTayAsked:
Who is Participating?
 
Sinisa VukConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.