I'm converting a D2006 program to D2010. I have a value stored in a single byte per character string in my database and I need to load it into a control that has a LoadFromStream, so my plan was to write the string to a stream and use that with LoadFromStream. But it did not work. In studying the problem, I see an issue that tells me that I don't really understand how conversion from AnsiString to Unicode string works. Here is a piece of standalone code that illustrates the issue I am confused by:;
procedure TForm1.Button1Click(Sender: TObject); {$O-}
var
sBuffer: String;
oStringStream: TStringStream;
sAnsiString: AnsiString;
sUnicodeString: String;
iSize1,
iSize2: Word;
begin
sAnsiString := '12345';
oStringStream := TStringStream.Create(sBuffer);
sUnicodeString := sAnsiString;
iSize1 := StringElementSize(sAnsiString);
iSize2 := StringElementSize(sUnicodeString);
oStringStream.WriteString(sUnicodeString);
end;
If you break on the last line, and inspect the Bytes property of oStringStream, you will see that it looks like this:
Bytes (49 {$31}, 50 {$32}, 51 {$33}, 52 {$34}, 53 {$35}
I was expecting that it might look something like
(49 {$31}, 00 {$00}, 50 {$32}, 00 {$00}, 51 {$33}, 00 {$00},
52 {$34}, 00 {$00}, 53 {$35}, 00 {$00} ...
Apparently my expectations are in error. But then, how to convert an AnsiString to unicode?
I'm not getting the right results out of the LoadFromStream because it is reading from the stream two bytes at a time, but the data it is receiving is not arranged that way. What is it that I should do to give the LoadFromStream a well formed stream of data based on a unicode string?
Thank you for your help.